iOS开发 -------- 图片浏览器初步
一 示例代码
1 // 2 // RootViewController.m 3 // 图片浏览器初步 4 // 5 // Created by lovestarfish on 15/11/1. 6 // Copyright © 2015年 S&G. All rights reserved. 7 // 8 9 #import "RootViewController.h" 10 11 #define POTOIMGW 175 12 #define POTOIMGH 300 13 #define POTOIMGX 100 14 #define POTOIMGY 120 15 16 @interface RootViewController () 17 18 @property (nonatomic,retain) UILabel *firstLabel; 19 @property (nonatomic,retain) UILabel *lastLabel; 20 @property (nonatomic,retain) UIImageView *icon; 21 @property (nonatomic,retain) UIButton *leftButton; 22 @property (nonatomic,retain) UIButton *rightButton; 23 24 @property (nonatomic,retain) NSArray *dataSource; 25 26 - (void)change; 27 @property (nonatomic,assign) int i; 28 29 @end 30 31 @implementation RootViewController 32 33 - (void)dealloc { 34 self.firstLabel = nil; 35 self.lastLabel = nil; 36 self.icon = nil; 37 self.leftButton = nil; 38 self.rightButton = nil; 39 self.dataSource = nil; 40 [super dealloc]; 41 } 42 43 - (NSArray *)dataSource { 44 if (!_dataSource) { 45 NSDictionary *dic1 = @{@"name":@"xib1",@"desc":@"第一张"}; 46 NSDictionary *dic2 = @{@"name":@"xib2",@"desc":@"第二张"}; 47 NSDictionary *dic3 = @{@"name":@"xib3",@"desc":@"第三张"}; 48 NSDictionary *dic4 = @{@"name":@"xib4",@"desc":@"第四张"}; 49 NSDictionary *dic5 = @{@"name":@"xib5",@"desc":@"第五张"}; 50 self.dataSource = @[dic1,dic2,dic3,dic4,dic5]; 51 } 52 return _dataSource; 53 } 54 55 - (void)viewDidLoad { 56 [super viewDidLoad]; 57 //初始化 i= 0 58 self.i = 0; 59 60 //创建一个用来显示序号的label控件 61 UILabel *headLabel =[[UILabel alloc] initWithFrame:CGRectMake(50, 80, 275, 30)]; 62 [headLabel setTextAlignment:NSTextAlignmentCenter]; 63 [headLabel setTextColor:[UIColor blackColor]]; 64 [self.view addSubview:headLabel]; 65 [headLabel release]; 66 self.firstLabel = headLabel; 67 68 //创建一个装载图片的控件 69 UIImageView *potoImage = [[UIImageView alloc] initWithFrame:CGRectMake(POTOIMGX, POTOIMGY, POTOIMGW, POTOIMGH)]; 70 potoImage.image = [UIImage imageNamed:@"xib1"]; 71 [self.view addSubview:potoImage]; 72 [potoImage release]; 73 self.icon = potoImage; 74 75 //创建最下边的描述图片的label控件 76 UILabel *desLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 450, 275, 30)]; 77 desLabel.textAlignment = NSTextAlignmentCenter; 78 [self.view addSubview:desLabel]; 79 [desLabel release]; 80 self.lastLabel = desLabel; 81 82 //创建两个方向键按钮 83 UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 84 leftBtn.frame = CGRectMake(30, 250, 40, 40); 85 [leftBtn setTitle:@"向左" forState:UIControlStateNormal]; 86 [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside]; 87 [self.view addSubview:leftBtn]; 88 self.leftButton = leftBtn; 89 90 UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem]; 91 rightBtn.frame = CGRectMake(POTOIMGX + POTOIMGW + 30, 250, 40, 40); 92 [rightBtn setTitle:@"向右" forState:UIControlStateNormal]; 93 [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside]; 94 [self.view addSubview:rightBtn]; 95 self.rightButton = rightBtn; 96 97 [self change]; 98 } 99 100 /** 101 * 点击按钮图片和文字做出相应的改变 102 */ 103 - (void)change { 104 105 self.icon.image = [UIImage imageNamed:self.dataSource[self.i][@"name"]]; 106 self.lastLabel.text = self.dataSource[self.i][@"desc"]; 107 self.firstLabel.text = [NSString stringWithFormat:@"%d/5",self.i + 1]; 108 /* 109 self.firstLabel.text = [NSString stringWithFormat:@"%d/5",self.i + 1]; 110 switch (self.i) { 111 case 0: 112 self.lastLabel.text = @"一"; 113 self.icon.image = [UIImage imageNamed:@"xib1"]; 114 break; 115 case 1: 116 self.lastLabel.text = @"二"; 117 self.icon.image = [UIImage imageNamed:@"xib2"]; 118 break; 119 case 2: 120 self.lastLabel.text = @"三"; 121 self.icon.image = [UIImage imageNamed:@"xib3"]; 122 break; 123 case 3: 124 self.lastLabel.text = @"四"; 125 self.icon.image = [UIImage imageNamed:@"xib4"]; 126 break; 127 case 4: 128 self.lastLabel.text = @"五"; 129 self.icon.image = [UIImage imageNamed:@"xib5"]; 130 break; 131 default: 132 break; 133 } 134 */ 135 //控制按钮的点击,如果为5则右键shixiao,如果为1,则左键实现 136 self.leftButton.enabled = (self.i != 0); 137 self.rightButton.enabled = (self.i != 4); 138 } 139 140 /** 141 * 向右按钮的点击方法 142 */ 143 - (void)rightBtnClick:(UIButton *)button { 144 self.i++; 145 [self change]; 146 } 147 148 /** 149 * 向左按钮的点击方法 150 */ 151 - (void)leftBtnClick:(UIButton *)button { 152 self.i--; 153 [self change]; 154 } 155 156 - (void)didReceiveMemoryWarning { 157 [super didReceiveMemoryWarning]; 158 159 } 160 161 @end
二 实现效果