视图切换的动画片效果
视图切换的动画效果
为了避免视图之间切换的呆板问题,在IPHONE中引入了转换动画效果,分别在UIKit.framework和QuartzCore.framework中,后者的动画类型要比前者丰富一些。
- (IBAction)switchViews:(id)sender{ //准备动画 [UIView beginAnimations:@"animationID" context:nil]; //动画播放持续时间 [UIView setAnimationDuration:0.5f]; //动画速度 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationRepeatAutoreverses:NO]; UIButton *theButton = (UIButton *)sender; //动画方向 switch (theButton.tag) { case 0: [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES]; break; case 1: [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; break; case 2: [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; break; case 3: [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; break; default: break; } [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; [UIView commitAnimations]; }
下面的动画需要导入QuartzCore.framework库,并在实现文件中导入。
#import <QuartzCore/QuartzCore.h> - (IBAction)switchViews:(id)sender{ //准备动画 CATransition *animation = [CATransition animation]; animation.delegate = self; //动画播放持续时间 animation.duration = 0.5f; //动画速度 animation.timingFunction = UIViewAnimationCurveEaseInOut; animation.fillMode = kCAFillModeForwards; animation.removedOnCompletion = NO; UIButton *theButton = (UIButton *)sender; //动画效果 switch (theButton.tag) { case 0: animation.type = @"cube"; break; case 1: animation.type = @"suckEffect"; break; case 2: animation.type = @"oglFlip"; break; case 3: animation.type = @"rippleEffect"; break; case 4: animation.type = @"pageCurl"; break; case 5: animation.type = @"pageUnCurl"; break; case 6: animation.type = @"cameraIrisHollowOpen "; break; case 7: animation.type = @"cameraIrisHollowClose "; break; default: break; } [self.view.layer addAnimation:animation forKey:@"animation"]; [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; }
下面的动画同样需要导入QuartzCore.framework库,并在实现文件中导入。
#import <QuartzCore/QuartzCore.h> - (IBAction)switchViews:(id)sender{ CATransition *animation = [CATransition animation]; animation.duration = 0.5f; animation.timingFunction = UIViewAnimationCurveEaseInOut; animation.fillMode = kCAFillModeForwards; UIButton *theButton = (UIButton *)sender; switch (theButton.tag) { case 0: /*动画效果 kCATransitionFade kCATransitionMoveIn kCATransitionPush kCATransitionReveal */ animation.type = kCATransitionPush; /*动画方向 kCATransitionFromRight kCATransitionFromLeft kCATransitionFromTop kCATransitionFromBottom */ animation.subtype = kCATransitionFromTop; break; case 1: animation.type = kCATransitionMoveIn; animation.subtype = kCATransitionFromTop; break; case 2: animation.type = kCATransitionReveal; animation.subtype = kCATransitionFromTop; break; case 3: animation.type = kCATransitionFade; animation.subtype = kCATransitionFromTop; break; default: break; } [self.view.layer addAnimation:animation forKey:@"animation"]; }
UIKit.framework中的动画是对UIView的,而QuartzCore.framework是针对视图的属性layer来实现的,后者与视图动画比起来,具备更大的优势,更容易进行转换,倾斜,放大,缩小等等。