设置uiview简单的动画
新建一个single view工程,在ViewController中添加两个view和一个button,两个view颜色不同,用button来控制两个view切换
- (void)viewDidLoad{[super viewDidLoad];UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];view.backgroundColor = [UIColor redColor];[self.view addSubview:view];UIView *view2= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];view2.backgroundColor = [UIColor blueColor];[self.view addSubview:view2];UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];btn.frame = CGRectMake(100, 100, 100, 40);[self.view addSubview:btn];[btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];}
设置button click事件
- (void)buttonClick{[UIView beginAnimations:nil context:nil];//开始
[UIView setAnimationDuration:20.0];//动画时长[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];//第一个参数是动画效果,第二个是哪个视图进行动画,第三个是缓存
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置渐变效果[UIView setAnimationDelegate:self];//设置代理[UIView setAnimationWillStartSelector:@selector(willStartAnimation)];[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];[UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
[UIView commitAnimations];//提交动画}
其中setAnimationDidStopSelector和setAnimationWillStartSelector可以设置动画结束和开始的时候调用的事件,比如我们可以将结束时的选择器设置为@selector(buttonClick),这样动画就会循环;当然[UIView setAnimationRepeatCount:3];也可以让动画重复,但是两者不同,前者是红渐变到蓝,再由蓝渐变到红,后者是由红渐变到蓝,然后突然变红,再渐变到蓝。