今天看到项目里面代码,有些UIImageView的动画,所以动手写了下
1.UIImageView一些可能不常用的与动画有关的属性
@property(nonatomic,copy) NSArray *animationImages; // The array must contain UIImages. Setting hides the single image. default is nil
@property(nonatomic,copy) NSArray *highlightedAnimationImages NS_AVAILABLE_IOS(3_0); // The array must contain UIImages. Setting hides the single image. default is nil
@property(nonatomic) NSTimeInterval animationDuration; // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)
@property(nonatomic) NSInteger animationRepeatCount; // 0 means infinite (default is 0)
- (void)startAnimating;
- (void)stopAnimating;
- (BOOL)isAnimating;
2.代码实现:有两个button控制 start,stop
//1.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
self.animationImageView = imageView;
animationImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
nil];
animationImageView.animationDuration = 1.5;
animationImageView.animationRepeatCount = 0;
[self.view addSubview:animationImageView];
[imageView release];
UIButton *startAnimationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[startAnimationButton setFrame:CGRectMake(100, 200, 50, 50)];
[startAnimationButton setTitle:@"start" forState:UIControlStateNormal];
[startAnimationButton addTarget:self action:@selector(startAnimationImageView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startAnimationButton];
UIButton *endAnimationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[endAnimationButton setFrame:CGRectMake(100, 300, 50, 50)];
[endAnimationButton setTitle:@"stop" forState:UIControlStateNormal];
[endAnimationButton addTarget:self action:@selector(endAnimationImageView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:endAnimationButton];
- (void)startAnimationImageView
{
[self.animationImageView startAnimating];
}
- (void)endAnimationImageView
{
[self.animationImageView stopAnimating];
}
//参考:http://blog.youkuaiyun.com/maojudong/article/details/6898430