标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (strong, nonatomic) UIImageView *imgVAnimation; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)animationByNormalOperation; 6 - (void)animationByBlockOperation; 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 [self layoutUI]; 15 } 16 17 - (void)didReceiveMemoryWarning { 18 [super didReceiveMemoryWarning]; 19 // Dispose of any resources that can be recreated. 20 } 21 22 - (void)layoutUI { 23 self.view.backgroundColor = [UIColor blackColor]; 24 NSString *path = [[NSBundle mainBundle] pathForResource:@"Star" ofType:@"png"]; 25 _imgVAnimation = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]]; 26 _imgVAnimation.frame = CGRectMake(0, 0, 20.0, 20.0); 27 _imgVAnimation.center = CGPointMake(10.0, 30.0); 28 [self.view addSubview:_imgVAnimation]; 29 } 30 31 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 32 _imgVAnimation.center = CGPointMake(10.0, 30.0); 33 34 [self animationByBlockOperation]; 35 } 36 37 /** 38 * 普通操作设置动画 39 */ 40 - (void)animationByNormalOperation { 41 [UIView beginAnimations:nil context:NULL]; 42 [UIView setAnimationDuration:1.5]; //设置动画持续时间;默认值为0.2s 43 [UIView setAnimationDelay:1.0]; //设置动画延迟时间;默认值为0.0s 44 [UIView setAnimationRepeatCount:5.0]; //设置动画播放重复次数;默认值为0,表示不循环只执行一次(区别于UIImageView.animationRepeatCount=0为无限循环) 45 _imgVAnimation.center = CGPointMake(220, 400); 46 [UIView commitAnimations]; 47 } 48 49 /** 50 * 代码块设置动画 51 */ 52 - (void)animationByBlockOperation { 53 [UIView animateWithDuration:1.5 54 delay:1.0 55 options:UIViewAnimationOptionTransitionNone 56 animations:^{ 57 [UIView setAnimationRepeatCount:5.0]; 58 _imgVAnimation.center = CGPointMake(220, 400); 59 } completion:^(BOOL finished) { 60 61 }]; 62 } 63 64 @end
161设置在屏幕中的动画延迟(扩展知识:普通操作和代码块操作实现动画效果)
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4583864.html