1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @property (retain, nonatomic) NSArray *pic; 6 @property (assign, nonatomic) NSInteger index; 7 @property (weak, nonatomic) IBOutlet UILabel *lblIndex; 8 @property (weak, nonatomic) IBOutlet UILabel *lblTitle; 9 @property (weak, nonatomic) IBOutlet UIImageView *picture;10 - (IBAction)nextPicture:(id)sender;11 @property (weak, nonatomic) IBOutlet UIButton *btnnext;12 13 - (IBAction)lastPicture:(id)sender;14 @property (weak, nonatomic) IBOutlet UIButton *btnLast;15 16 @end17 @implementation ViewController18 19 20 - (void)viewDidLoad {21 [super viewDidLoad];22 //让第一个图片显示出来,所以先调用一次23 [self nextPicture:nil];24 }25 26 - (void)didReceiveMemoryWarning {27 [super didReceiveMemoryWarning];28 }29 30 // 重写pic属性get方法---懒加载数据31 - (NSArray *)pic32 { /*33 写代码加载picture.plist文件中的数据到_pic;34 1.获取picture.plist文件的路径35 ps:[NSBundle mainBundle]获取这个app安装到手机上时的根目录36 然后在根目录下搜索picture.plist文件路径37 */38 if (_pic == nil) {39 NSString *string = [[NSBundle mainBundle] pathForResource:@"picture" ofType:@".plist"];40 NSArray *array = [NSArray arrayWithContentsOfFile:string];41 //NSLog(@"%@",array);42 43 _pic = array;44 45 }46 return _pic;47 }48 49 - (IBAction)nextPicture:(id)sender {50 //1.让索引自加51 _index++;52 //2.从数组中获取当前这张图片的数据53 NSDictionary *dict = self.pic[self.index-1];54 //3.把获取到得数据设置给界面上的控件55 self.lblIndex.text = [NSString stringWithFormat:@"%ld/%ld",self.index,self.pic.count];56 //4.通过image属性来设置图片框里面的图片57 self.picture.image =[UIImage imageNamed: dict[@"picture"]];58 59 self.lblTitle.text = dict[@"title"];60 //设置“下一张”按钮是否可以点击61 self.btnnext.enabled =( _index != self.pic.count);62 //设置“上一张”按钮是否可以点击63 self.btnLast.enabled =( _index-1 != 0);64 65 }66 67 - (IBAction)lastPicture:(id)sender {68 _index--;69 NSDictionary *dict = self.pic[self.index-1];70 self.lblIndex.text = [NSString stringWithFormat:@"%ld/%ld",self.index,self.pic.count];71 self.picture.image =[UIImage imageNamed: dict[@"picture"]];72 self.lblTitle.text = dict[@"title"];73 74 self.btnLast.enabled =( _index-1 != 0);75 self.btnnext.enabled =( _index != self.pic.count);76 }77 @end
开发思路:
1.完成基本功能
2.考虑性能
(1)(初始化操作,可以直接调用change进行)
(2)因为要控制序号和图片两个变量,所以考虑使用字典代替掉switch
(3)每次点击,字典都需要创建一次,效率地下,可以考虑创建的这部分拿到初始化方法中去,这样就只需要创建一次就ok了。
(4)考虑缺点(对代码的顺序要求极其严格)
(5)懒加载(需要的时候才加载,那么什么时候是需要的时候,及调用get方法的时候)
(6)每次都来一下?效率低下—》只有第一次调用get方法时为空,此时实例化并建立数组,其他时候直接返回成员变量(仅仅执行一次)
注意点:
1.方法的调用堆栈(顺序)。
2.使用plist:让数据的操作更加灵活,把数据弄到外面去,解除耦合性,让耦合性不要太强。实际上是一个xml,是苹果定义的一种特殊格式的xml。
3.bundle-包(只读)
二、Tomcat简单实现(吃小鸟)
1 - (IBAction)eatBird:(id)sender { 2 3 [self startAnimating:40 picName:@"cat_eat"]; 4 } 5 6 - (void)startAnimating:(int)count picName:(NSString *)picName 7 { 8 // 如果当前图片框正在执行动画,那么直接return,什么也不做 9 if (self.imageViewCat.isAnimating) {10 return;11 }12 // 1.加载图片到数组中13 NSMutableArray *array = [NSMutableArray array];14 for (int i=0; i