iOS中的转场动画

来源:互联网 发布:看斗破苍穹小说软件 编辑:程序博客网 时间:2024/06/06 11:53

在iOS开发中有时会有一些动画的需求,本篇博客我们说一下动画效果。本篇博客中的动画是动画中的一种--转场动画(CATransition)。

1.为导航控制器添加动画。

在一般的开发中在一个控制器push到下一个控制器的时候苹果会有一个默认的动画即下一个控制器平移过来将上一个控制器覆盖,大多数的应用也使用了苹果给出的默认动画效果。然而有些项目在一个控制器push到下一个控制器的时候却需要添加一些特殊的动画效果。如何添加这些特殊动画呢?我们实际是将这些动画添加到了控制器的视图图层上。看代码:

CATransition * transition = [CATransitionanimation];

    /*

     pageCurl            向上翻页

     pageUnCurl          向下翻页

     rippleEffect        滴水效果

     suckEffect          收缩效果,如一块布被抽走

     cube                立方体效果

     oglFlip             上下翻转效果

     */

    transition.type =@"cube"//动画效果

    transition.duration =1;    // 动画持续时间

    /*

     kCATransitionFromTop

     

     kCATransitionFromBottom

     

     kCATransitionFromRight

     

     kCATransitionFromLeft

     */

    transition.subtype =kCATransitionFromRight;    // 动画子类型,动画的方向

    transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];  // 动画的轨迹模式

    

    [self.navigationController.view.layeraddAnimation:transition forKey:nil];

    NextViewController * nextVC = [[NextViewControlleralloc] init];

    [self.navigationControllerpushViewController:nextVC animated:YES];

在使用了以上的这些代码之后我们在进行push的时候就会有不一样的动画效果了。

效果如下图:

这是一个立方体的效果。

   二. 为切换视图添加动画

有时我们在切换两个视图的时候也需要添加动画,实际动画还是添加在了图层上了。切换的两个视图是添加在同一个视图控制器的view上的。看代码:

    CGContextRef context =UIGraphicsGetCurrentContext();

    [UIViewbeginAnimations:nilcontext:context];

    [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

    [UIViewsetAnimationDuration:kDuration];

    switch (tag) {

        case105:

            [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlDownforView:self.viewcache:YES];

            break;

        case106:

            [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:self.viewcache:YES];

            break;

        case107:

            [UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromLeftforView:self.viewcache:YES];

            break;

        case108:

            [UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromRightforView:self.viewcache:YES];

            break;

        default:

            break;

    }

    

    NSUInteger green = [[self.viewsubviews] indexOfObject:self.greenView];

    NSUInteger blue = [[self.viewsubviews] indexOfObject:self.blueView];

    [self.viewexchangeSubviewAtIndex:green withSubviewAtIndex:blue];

    

    [UIViewsetAnimationDelegate:self];

    // 动画完毕后调用某个方法

    //[UIView setAnimationDidStopSelector:@selector(animationFinished:)];

    [UIViewcommitAnimations];

下面的代码动画效果会更丰富一些:

CATransition *animation = [CATransitionanimation];

    animation.delegate =self;

    animation.duration =kDuration;

    animation.timingFunction =UIViewAnimationCurveEaseInOut;

    

    switch (tag) {

        case101:

            animation.type =kCATransitionFade;

            break;

        case102:

            animation.type =kCATransitionPush;

            break;

        case103:

            animation.type =kCATransitionReveal;

            break;

        case104:

            animation.type =kCATransitionMoveIn;

            break;

        case201:

            animation.type =@"cube";

            break;

        case202:

            animation.type =@"suckEffect";

            break;

        case203:

            animation.type =@"oglFlip";

            break;

        case204:

            animation.type =@"rippleEffect";

            break;

        case205:

            animation.type =@"pageCurl";

            break;

        case206:

            animation.type =@"pageUnCurl";

            break;

        case207:

            animation.type =@"cameraIrisHollowOpen";

            break;

        case208:

            animation.type =@"cameraIrisHollowClose";

            break;

        default:

            break;

    }

    animation.subtype =kCATransitionFromRight;

    

    

    NSUInteger green = [[self.viewsubviews] indexOfObject:self.greenView];

    NSUInteger blue = [[self.viewsubviews] indexOfObject:self.blueView];

    [[self.viewlayer] addAnimation:animationforKey:@"animation"];

    [self.viewexchangeSubviewAtIndex:green withSubviewAtIndex:blue];


1 0
原创粉丝点击