标签:
效果如下:
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)startAnimation; 6 - (void)animationWillStart:(NSString *)animationID context:(void *)context; 7 - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context; 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 [self layoutUI]; 16 } 17 18 - (void)didReceiveMemoryWarning { 19 [super didReceiveMemoryWarning]; 20 // Dispose of any resources that can be recreated. 21 } 22 23 - (void)layoutUI { 24 self.view.backgroundColor = [UIColor blackColor]; 25 26 _imgVAnimation = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ImageForAnimation"]]; 27 _imgVAnimation.center = CGPointMake(self.view.center.x, 100); 28 [self.view addSubview:_imgVAnimation]; 29 } 30 31 - (void)viewWillAppear:(BOOL)animated { 32 [super viewWillAppear:animated]; 33 34 [self startAnimation]; 35 } 36 37 - (void)startAnimation { 38 _imgVAnimation.center = CGPointMake(self.view.center.x, 100); 39 _imgVAnimation.alpha = 1.0; 40 _imgVAnimation.transform = CGAffineTransformIdentity; 41 42 [UIView beginAnimations:nil context:NULL]; 43 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //设置动画弧度;默认值为UIViewAnimationCurveEaseInOut 44 [UIView setAnimationDuration:2.0]; 45 [UIView setAnimationRepeatCount:1.0]; //设置动画播放重复次数;默认值为0,表示不循环只执行一次(区别于UIImageView.animationRepeatCount=0为无限循环) 46 [UIView setAnimationRepeatAutoreverses:YES]; //设置自动倒退,即动画回放;默认值为NO,只在动画起码执行一次的情况下有效 47 [UIView setAnimationDelegate:self]; 48 [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)]; //设置动画将开始时执行委托方法;在设置好委托前提下,默认就是绑定animationWillStart:context: 49 [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; //设置动画结束后执行委托方法;在设置好委托前提下,默认就是绑定animationDidStop:finished:context: 50 51 _imgVAnimation.center = CGPointMake(self.view.center.x, 300); 52 _imgVAnimation.alpha = 0.2; 53 //设置放大和旋转 54 CGAffineTransform transformScale = CGAffineTransformScale(CGAffineTransformIdentity, 3, 3); 55 CGAffineTransform transformRotate = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI); //M_PI表示180度 56 _imgVAnimation.transform = CGAffineTransformConcat(transformScale, transformRotate); 57 58 [UIView commitAnimations]; 59 } 60 61 - (void)animationWillStart:(NSString *)animationID context:(void *)context { 62 static BOOL isFirstTime = YES; 63 if (isFirstTime) { 64 UIAlertView *alertVAnimation = [[UIAlertView alloc] initWithTitle:@"提示信息" 65 message:@"开始动画了" 66 delegate:nil 67 cancelButtonTitle:nil 68 otherButtonTitles:@"确定", nil]; 69 [alertVAnimation show]; 70 71 isFirstTime = NO; 72 } 73 } 74 75 - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 76 //如果动画正常结束时(非强制取消),就再次开始动画 77 if ([finished boolValue]) { 78 [self startAnimation]; 79 } 80 } 81 82 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4583897.html