下边是视图切换时的部分代码:
- (IBAction)buttonPressed1:(id)sender {
UIButton *button = (UIButton *)sender;
NSInteger tag = button.tag;
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = kDuration;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
NSLog(@"button tag is %i",tag);
//上边按钮中的下边四个和下边的两行八个按钮,共12个button的tag
switch (tag) {
case 101://淡化
animation.type = kCATransitionFade;
break;
case 102://推挤
animation.type = kCATransitionPush;
break;
case 103://揭开
animation.type = kCATransitionReveal;
break;
case 104://覆盖
animation.type = kCATransitionMoveIn;
break;
case 201://立方体
animation.type = @"cube";
break;
case 202://吸收
animation.type = @"suckEffect";
break;
case 203://翻转
animation.type = @"oglFlip";
break;
case 204://波纹
animation.type = @"rippleEffect";
break;
case 205://翻页
animation.type = @"pageCurl";
break;
case 206://反翻页
animation.type = @"pageUnCurl";
break;
case 207://镜头开
animation.type = @"cameraIrisHollowOpen";
break;
case 208://镜头关
animation.type = @"cameraIrisHollowClose";
break;
default:
break;
}
switch (self.typeID) {
case 0://
animation.subtype = kCATransitionFromLeft;
break;
case 1:
animation.subtype = kCATransitionFromBottom;
break;
case 2:
animation.subtype = kCATransitionFromRight;
break;
case 3:
animation.subtype = kCATransitionFromTop;
break;
default:
break;
}
self.typeID += 1;
if (self.typeID > 3) {
self.typeID = 0;
}
NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
[[self.view layer] addAnimation:animation forKey:@"animation"];
}
#pragma mark UIView动画
- (IBAction)buttonPressed2:(id)sender {
UIButton *button = (UIButton *)sender;
NSInteger tag = button.tag;
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:kDuration];
NSLog(@"button tag is %i",tag);
//这里是最上边的四个按钮的tag
switch (tag) {
case 105://下翻
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
break;
case 106://上翻
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
break;
case 107://左转
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
break;
case 108://右转
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
break;
default:
break;
}
NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
[UIView setAnimationDelegate:self];
// 动画完毕后调用某个方法
//[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
[UIView commitAnimations];
}
这里定义了两个视图,一个greenView,一个blueView。切换,详情还是你们自已看Demo吧。
2.使用CATransition对象来实现:
CATransition比较强大,一般可以使用CATransition模拟UIView的动画。
关于UIView动画:
UIView动画是应用在一个view上面的。
关于CABasicAnimation动画:
CABasicAnimation动画是应用在一个layer上面的。