IOS动画(5)转场动画

看舞台剧时当需要更改舞台背景时,舞台大幕会缓缓合上,然后缓缓打开,舞台上的内容就变了。

转场动画的意义就类似于舞台大幕,当layer上的内容变化时,我们希望通过一种动画的效果来切换,CoreAnimation提供了CATransition实现转场效果。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 120, 120)];
    imageView.image = [UIImage imageNamed:@"5.png"];
    [self.view addSubview:imageView];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 120, 120)];
    imageView2.image = [UIImage imageNamed:@"4.png"];
    [self.view addSubview:imageView2];
}



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromLeft;
  
 transition.duration = 1;
    [self.view.layer addAnimation:transition forKey:nil];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];

}

 CATransition中又两个关键参数需要设置:type和subtype,前者控制转场动画的类型,后者控制转场动画的方向

type支持以下取值

kCATransitionFade  渐隐效果

kCATransitionMoveIn  移入效果

kCATransitionPush  推入效果

kCATransitionReveal  揭开效果

除了上面四种值,type还支持以下的私有动画

cube  立方体旋转

suckEffect  被吸入效果

oglFlip  翻转

rippleEffect  水波动画

pageCurl  页面揭开

pageUnCurl  页面放下

cameraIrisHollowOpen  镜头打开

cameraIrisHollowClose  镜头关闭

 

subtype提供四个动画方向,支持以下取值

kCATransitionFromRight

kCATransitionFromLeft

kCATransitionFromTop

kCATransitionFromBottom

 

此外,也可以通过UIView的beginAnimation:context:和commitAnimation实现转场动画

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 120, 120)];
    imageView.image = [UIImage imageNamed:@"5.png"];
    [self.view addSubview:imageView];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 120, 120)];
    imageView2.image = [UIImage imageNamed:@"4.png"];
    [self.view addSubview:imageView2];
}



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
    [UIView commitAnimations];
}

setAniamtionTransition支持以下属性

UIViewAnimationTransitionNone  无动画

UIViewAnimationTransitionFlipFromLeft  从左边滑入

UIViewAnimationTransitionFlipFromRight  从右边滑入

UIViewAnimationTransitionCurlUp  翻开书页

UIViewAnimationTransitionCurlDown  放下书页

 

setAniamtionCurve支持以下属性

UIViewAnimationCurveEaseInOut         // slow at beginning and end

UIViewAnimationCurveEaseIn            // slow at beginning

UIViewAnimationCurveEaseOut           // slow at end

UIViewAnimationCurveLinear

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。