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 @end 17 @implementation ViewController 18 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 *)pic 32 { /* 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<count; i++) { 15 // 拼接图片名称 16 NSString *imagName = [NSString stringWithFormat:@"%@%04d.jpg",picName,i]; 17 /* 18 根据图片名称加载图片 19 通过imageNamed:这种方式加载图片,加载好的图片会一直保存写在内存中,不会释放。 20 优点:下次如果再使用同样的图片时就不需要重新加载了,因为内存也已有 21 缺点:如果加载大量的图片,那么这些图片会一直保存在内存中,导致应用程序占用内存过大(这叫缓存) 22 //UIImage *imageCat = [UIImage imageNamed:imagName]; 23 */ 24 25 // 解决:换一种加载图片的方式,不需要使用缓存 26 // 获取图片完整的路径 27 NSString *path = [[NSBundle mainBundle] pathForResource:imagName 28 ofType:nil]; 29 // 这里的参数不能传递图片名称了,这里需要传递图片的完整路径 30 UIImage *imageCat = [UIImage imageWithContentsOfFile:path]; 31 32 // 把图片加载到数组中 33 [array addObject:imageCat]; 34 } 35 // 2.设置图片的UIImageView(图片框)的animationimage属性 36 self.imageViewCat.animationImages = array; 37 // 3.设置动画持续时间 38 self.imageViewCat.animationDuration = self.imageViewCat.animationImages.count * 0.1; 39 40 // 4.设置动画是否重播 41 self.imageViewCat.animationRepeatCount = 1; 42 // 5.开启动画 43 [self.imageViewCat startAnimating]; 44 45 /* 46 清空图片集合 47 这样写的问题是,当动画启动以后,动画还没有执行,就已经让图片集合清空了,也就是说 48 self.imageViewCat.animationImages里面已经没有图片了,所以动画就不执行了。 49 self.imageViewCat.animationImages = nil; 50 想法:需要延迟一段时间执行,当动画执行完毕后再清空这些图片 51 */ 52 53 //设置图片框在调用setAnimationImages:nil方法的时候延迟执行 54 [self.imageViewCat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imageViewCat.animationImages.count*0.1]; 55 }