1.CATransition
CATransition是CAAnimation的子类,用于过渡动画或转场动画。为视图层移入移除屏幕提供转场动画。首先来看一下简单的Demo:
CATransition *animation = [CATransition animation]; animation.type = kCATransitionFade; animation.subtype = kCATransitionFromRight; animation.duration = 1.0; // 在window上执行CATransition, 即可在ViewController转场时执行动画 [self.view.window.layer addAnimation:animation forKey:@"kTransitionAnimation"]; AViewController *vc = [[AViewController alloc] init]; [self presentViewController:vc animated:NO completion:nil];
将该动画添加到window.layer上,则会present或push时使用指定的转场动画。
其中最主要的两个属性就是type和subtype。
-
type:转场动画的类型。
官方SDK只提供了四种转场动画的类型,即:
CA_EXTERN NSString * const kCATransitionFade;CA_EXTERN NSString * const kCATransitionMoveIn;CA_EXTERN NSString * const kCATransitionPush;CA_EXTERN NSString * const kCATransitionReveal;
私有的type:
NSString *const kCATransitionCube = @"cube"; NSString *const kCATransitionSuckEffect = @"suckEffect"; NSString *const kCATransitionOglFlip = @"oglFlip"; NSString *const kCATransitionRippleEffect = @"rippleEffect"; NSString *const kCATransitionPageCurl = @"pageCurl"; NSString *const kCATransitionPageUnCurl = @"pageUnCurl"; NSString *const kCATransitionCameraIrisHollowOpen = @"cameraIrisHollowOpen";NSString *const kCATransitionCameraIrisHollowClose = @"cameraIrisHollowClose";
-
subtype:动画类型的方向
CA_EXTERN NSString * const kCATransitionFromRight;CA_EXTERN NSString * const kCATransitionFromLeft;CA_EXTERN NSString * const kCATransitionFromTop;CA_EXTERN NSString * const kCATransitionFromBottom;
上面讲的是给window.layer添加transition,这样使得在present或push时使用指定的转场动画。
既然讲到这里了,就看一下把transition加在layer上。
看一下示例代码:
- (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)]; [button setTitle:@"进入" forState:UIControlStateNormal]; button.backgroundColor = [UIColor redColor]; [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 150, 150)]; [self.view addSubview:_imageView]; _imageView.backgroundColor = [UIColor redColor]; _imageArray = @[[UIImage imageNamed:@"成果秀1"],[UIImage imageNamed:@"点赞他人1"],[UIImage imageNamed:@"偷师学艺1"],[UIImage imageNamed:@"学会欣赏3"]]; _imageView.image = [UIImage imageNamed:@"成果秀1"];}- (void)buttonClicked{ CATransition *animation = [CATransition animation]; animation.type = @"cube"; animation.subtype = kCATransitionFromRight; animation.duration = 1.0; //换图片的时候使用转场动画 [self.imageView.layer addAnimation:animation forKey:nil]; //cycle to next image UIImage *currentImage = self.imageView.image; NSUInteger index = [self.imageArray indexOfObject:currentImage]; index = (index + 1) % [self.imageArray count]; self.imageView.image = self.imageArray[index]; }