不会说话的汤姆猫
//通过imageView播放动画 //1.创建一个imageView UIImageView *imageView = [[UIImageView alloc] init]; imageView.frame = CGRectMake(0, 0, 320, 480); //2.准备要播放的图片 NSMutableArray *films = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < 81; ++i) { //拼接文件名 NSString *name = [[NSString alloc] initWithFormat:@"cat_knockout%04d.jpg", i]; //文件名不存在,返回nil UIImage *img = [UIImage imageNamed:name]; [films addObject:img]; } //将第一张图片设置为背景 imageView.image = films[0]; //3.将胶卷交给imageView进行播放 imageView.animationImages = films; //4.设置播放总时长 imageView.animationDuration = 4.0; //5.是否重复播放 imageView.animationRepeatCount = 1; //0 无限重复 //6.开始播放 //[imageView startAnimating]; //结束播放 //[imageView stopAnimating]; //添加手势 //1.创建手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; //2.添加事件处理 [tap addTarget:self action:@selector(startImageViewAnimating:)]; //3.将手势添加到指定控件上 [imageView addGestureRecognizer:tap]; //4.对于UIImageView,需要开启人机交互 imageView.userInteractionEnabled = YES; [self.window addSubview:imageView]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } - (void)startImageViewAnimating:(UITapGestureRecognizer *)sender { //获取当前点击的位置 //[sender locationInView:] //sender.view == 被点击的视图 UIImageView *v = (UIImageView *)sender.view; [v startAnimating]; }