1、使用计时器实现Tom猫喝牛奶代码:
@interface ZMSecondViewController () @end @implementation ZMSecondViewController //不要在视图控制器初始化方法中添加与界面相关的操作,self.view - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; //创建背景图片 catView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; catView.image=[UIImage imageNamed:@"drink_0.jpg"]; [self.view addSubview:catView]; //喝牛奶的Button drinkButton=[UIButton buttonWithType:UIButtonTypeSystem]; drinkButton.frame=CGRectMake(240, 290, 60, 60); [drinkButton addTarget:self action:@selector(drinkClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:drinkButton]; //milk图片 //子视图的原点坐标以父视图的坐标为准,依然为(0,0); milkView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)]; milkView.image=[UIImage imageNamed:@"milk.png"]; [drinkButton addSubview:milkView]; //返回第一页面 UIButton *goToFirstButton=[UIButton buttonWithType:UIButtonTypeSystem]; goToFirstButton.frame=CGRectMake(5, 420, 60, 40); [goToFirstButton setTitle:@"Back" forState:UIControlStateNormal]; [goToFirstButton addTarget:self action:@selector(goBackClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:goToFirstButton]; } //喝牛奶时间触发 - (void)drinkClick { //每次点击 喝牛奶的 button ,如果当前 tom没在喝牛奶,就创建定时器,让它喝牛奶;如果在,就没有操作; //timer 为空,表示 当前没有timer ,tom没在喝牛奶 if(_timer==nil) { _timer=[NSTimer scheduledTimerWithTimeInterval:0.12 target:self selector:@selector(drink) userInfo:nil repeats:YES]; //不调用 fire 方法时,创建定时器之后,会 计时,每隔0.15秒调用一次changePic方法; //调用 fire 方法时 ,创建定时器之后,先调用一次changePic方法,之后,计时,每隔0.15秒调用一次changePic方法 ; [_timer fire]; //还是这个方法主流,添加到NSRunLoop中运行 //将计时器添加到主线程,拖拽其他控件不会让计时器停止运行 [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; } } //计时器绑定,拼接图片连贯动作 - (void)drink { _count++; NSString *picture=[NSString stringWithFormat:@"drink_%d.jpg",_count]; catView.image=[UIImage imageNamed:picture]; if(_count==80) { //_conut 归0 ;为了下次点击 button时 ,tom可以再次喝牛奶 _count=0; //如果当前定时器不为空;但是 tom喝牛奶的操作已经结束,_timer依旧在后台运行 ;需要把定时器销毁,赋空值 if(_timer!=nil) { // 不销毁timer,依旧在占用内存,timer销毁之后,不为空 //定时器销毁时,页面就定在最后一张图片显示 [_timer invalidate]; //赋空值,是为了 下次点击button的时候,根据空值判断,可以再次创建 timer,让tom再次喝牛奶 _timer =nil; } } }
@interface ZMSecondViewController () @end @implementation ZMSecondViewController { int i; UIImageView *catView; NSMutableArray *pictureArray; } //不要在视图控制器初始化方法中添加与界面相关的操作,self.view - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; //创建背景图片 catView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; catView.image=[UIImage imageNamed:@"drink_0.jpg"]; [self.view addSubview:catView]; //喝牛奶的Button UIButton *drinkButton=[UIButton buttonWithType:UIButtonTypeSystem]; drinkButton.frame=CGRectMake(240, 290, 60, 60); [drinkButton addTarget:self action:@selector(drinkClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:drinkButton]; //milk图片 //子视图的原点坐标以父视图的坐标为准,依然为(0,0); UIImageView *milkView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)]; milkView.image=[UIImage imageNamed:@"milk.png"]; [drinkButton addSubview:milkView]; //创建汤姆猫喝牛奶图片数组 pictureArray=[[NSMutableArray alloc] init]; for(i=1;i<81;i++) { NSString *imageName=[NSString stringWithFormat:@"drink_%d.jpg",i]; UIImage *imageDrink=[UIImage imageNamed:imageName]; [pictureArray addObject:imageDrink]; } //返回第一页面 UIButton *goToFirstButton=[UIButton buttonWithType:UIButtonTypeSystem]; goToFirstButton.frame=CGRectMake(5, 420, 60, 40); [goToFirstButton setTitle:@"Back" forState:UIControlStateNormal]; [goToFirstButton addTarget:self action:@selector(goBackClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:goToFirstButton]; } //喝牛奶触发事件 - (void)drinkClick { NSLog(@"喝牛奶"); if(i==80) { //停止动画 ;停止的时候,显示的图片是 _fireView最初赋值的图片 //与Timer区别在于,Timer停止时显示最后一张照片,而帧动画停止时显示最初赋值的照片 [catView stopAnimating]; i=0; } else { //设置图片切换一轮所需要的时间;默认值是 图片的数目* 1/30.0 ; catView.animationDuration=8; //设置图片切换的数组;数组中存放的UIImage的对象 catView.animationImages=pictureArray; //设置动画的重复次数,默认值为0,表示无穷大 catView.animationRepeatCount=1; //开始动画 [catView startAnimating]; } } @end