1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad { [super viewDidLoad];
[self creatBlockAnimation]; } #pragma mark - #pragma mark UIView的Block动画 -(void)creatBlockAnimation{ UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)]; myView.backgroundColor = [UIColor lightGrayColor]; myView.tag = 10; [self.view addSubview:myView]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(200, 50, 100, 100); button.backgroundColor = [UIColor lightGrayColor]; [button setTitle:@"开始" forState:UIControlStateNormal]; [button addTarget:self action:@selector(blockAnimation) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } -(void)blockAnimation{ UIView *myView = [self.view viewWithTag:10];
[UIView animateWithDuration:1 animations:^{ myView.frame = CGRectMake(50, 200, 50, 50); } completion:^(BOOL finished) { NSLog(@"动画结束"); }]; } #pragma mark - #pragma mark 传统的UIView动画 -(void)createUIView{ UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)]; myView.backgroundColor = [UIColor yellowColor]; myView.tag = 10; [self.view addSubview:myView]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(200, 50, 100, 100); button.backgroundColor = [UIColor lightGrayColor]; [button setTitle:@"开始" forState:UIControlStateNormal]; [button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } -(void)onClick{ UIView *myView = [self.view viewWithTag:10];
[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:1];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatCount:2]; [UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationWillStartSelector:@selector(animationWillStart)];
[UIView setAnimationDidStopSelector:@selector(animationDidStop)]; myView.frame = CGRectMake(50, 200, 100, 100);
[UIView commitAnimations]; } -(void)animationWillStart{ NSLog(@"动画将要开始"); } -(void)animationDidStop{ NSLog(@"动画已经结束"); } @end
|