直接上代码,看的不深,以后再做详细讲解
#import "WJJRootViewController.h"
@interface WJJRootViewController (){
//把view设为全局变量
UIView * _view;
}
@end
@implementation WJJRootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
_view = [[UIView alloc] initWithFrame:CGRectMake(300, 400, 20, 20)];
_view.tag = 250;
_view.backgroundColor = [UIColor purpleColor];
[self.view addSubview:_view];
//动画执行第一种方法
UIButton * button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.tag = 1;
button1.frame = CGRectMake(0, 460, 50, 20);
button1.backgroundColor = [UIColor yellowColor];
[button1 setTitle:@"动画1" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
//动画执行第一种方法
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeSystem];
button2.tag = 2;
button2.frame = CGRectMake(60, 460, 50, 20);
button2.backgroundColor = [UIColor yellowColor];
[button2 setTitle:@"动画2" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
//动画执行第一种方法
UIButton * button3 = [UIButton buttonWithType:UIButtonTypeSystem];
button3.tag = 3;
button3.frame = CGRectMake(120, 460, 50, 20);
button3.backgroundColor = [UIColor yellowColor];
[button3 setTitle:@"动画3" forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
}
- (void)change:(UIButton *)button{
if (button.tag == 1) {
//动画执行前的准备
[UIView beginAnimations:nil context:nil];
//动画执行的时间
[UIView setAnimationDuration:10];
//动画执行的 也就是要看到的动画效果
_view.frame = CGRectMake(0, 20, 50, 50);
_view.backgroundColor = [UIColor blackColor];
//动画执行
[UIView commitAnimations];
}else if (button.tag == 2){
//动画持续10s,block中写的就是要实现的最终动画结果
[UIView animateWithDuration:10 animations:^{
_view.frame = CGRectMake(0, 20, 50, 50);
_view.backgroundColor = [UIColor blackColor];
}];
}else if (button.tag == 3){
//第一个block是第一次实现的动画效果 第二个block是第一个动画效果完成后执行的另一个动画
[UIView animateWithDuration:4 animations:^{
_view.frame = CGRectMake(0, 20, 50, 50);
_view.backgroundColor = [UIColor blackColor];
} completion:^(BOOL finshed){
[UIView animateWithDuration:10 animations:^{
_view.frame = CGRectMake(300, 440, 20, 20);
_view.backgroundColor = [UIColor blackColor];
}];
}];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end