创建imageview那一部分就省略了,我用xib直接拖拽的。
先给imageview创建一个触摸手势。这里不要忘记给imageview设置userInteractionEnabled为YES。
//设置动画开始前的图片
appview.Imageview.image=[UIImage imageNamed:@"动态01.png"];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setImage)];
//设置imageview为可以响应事件
appview.Imageview.userInteractionEnabled = YES;
[appview.Imageview addGestureRecognizer:singleTap];
singleTap.delegate = self;//设置代理为自身
[singleTap setNumberOfTapsRequired:1];//1次点击
[singleTap setNumberOfTouchesRequired:1];//1个手指操作
接下来是触摸后响应的方法。
-(void)setImage{
NSLog(@"开始执行动画");
//动画结束后要显示的图片
appview.Imageview.image=[UIImage imageNamed:@"动态13.png"];
appview.Imageview.userInteractionEnabled = NO;
NSMutableArray *arrayM=[NSMutableArray array];
for (int i=0; i<13; i++) {
[arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"动态%02d.png",i+1]]];
}
//设置动画数组
[appview.Imageview setAnimationImages:arrayM];
//设置动画播放次数
[appview.Imageview setAnimationRepeatCount:1];
//设置动画播放时间
[appview.Imageview setAnimationDuration:8*0.1];
//开始动画
[appview.Imageview startAnimating];
//动画结束后响应事件(我这里是显示了一个关闭按钮)
//[self performSelector:@selector(over) withObject:nil afterDelay:0.8];
//清理动画数组
[appview.Imageview performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:appview.Imageview.animationDuration];
}
OK,搞定了。