首页 > 技术文章 > iOS CATransition 动画的简单使用

Jake-Chen-World 2017-03-16 15:00 原文

下面是实现的代码

//选择动画

- (IBAction)selectAnimationTypeButton:(id)sender {

    UIButton *button = sender;

    animationType animationType = button.tag;

    NSString *subtypeString;

    switch (_subType) {

        case 0:

            subtypeString = kCATransitionFromLeft;

            break;

        case 1:

            subtypeString = kCATransitionFromBottom;

            break;

        case 2:

            subtypeString = kCATransitionFromRight;

            break;

        case 3:

            subtypeString = kCATransitionFromTop;

            break;

        default:

            break;

    }

    

    _subType += 1;

    if (_subType > 3) {

        _subType = 0;

    }

    

    switch (animationType) {

        case Fade:

            [self transitionWithType:kCATransitionFade withSubtype:subtypeString forView:self.view];

            break;

        case Push:

            [self transitionWithType:kCATransitionPush withSubtype:subtypeString forView:self.view];

            break;

        case Reveal:

            [self transitionWithType:kCATransitionReveal withSubtype:subtypeString forView:self.view];

            break;

        case MoveIn:

            [self transitionWithType:kCATransitionMoveIn withSubtype:subtypeString forView:self.view];

            break;

        case Cube:

            [self transitionWithType:@"cube" withSubtype:subtypeString forView:self.view];

            break;

        case SuckEffect:

            [self transitionWithType:@"suckEffect" withSubtype:subtypeString forView:self.view];

            break;

        case OgleFlip:

            [self transitionWithType:@"oglFlip" withSubtype:subtypeString forView:self.view];

            break;

        case RippleEffect:

            [self transitionWithType:@"rippleEffect" withSubtype:subtypeString forView:self.view];

            break;

        case PageCurl:

            [self transitionWithType:@"pageCurl" withSubtype:subtypeString forView:self.view];

            break;

        case pageUnCurl:

            [self transitionWithType:@"pageUnCurl" withSubtype:subtypeString forView:self.view];

            break;

        case CameraIrisHollowOpen:

            [self transitionWithType:@"cameraIrisHollowOpen" withSubtype:subtypeString forView:self.view];

            break;

            case CameraIrisHollowClose:

            [self transitionWithType:@"cameraIrisHollowClose" withSubtype:subtypeString forView:self.view];

            break;

            case CurlDow:

            [self animationWithView:self.view withAnimationTransition:UIViewAnimationTransitionCurlDown];

            break;

        case CurlUp:

            [self animationWithView:self.view withAnimationTransition:UIViewAnimationTransitionCurlUp];

            break;

        case FlipFromLeft:

            [self animationWithView:self.view withAnimationTransition:(UIViewAnimationTransitionFlipFromLeft)];

            break;

        case FlipFromRight:

            [self animationWithView:self.view withAnimationTransition:(UIViewAnimationTransitionFlipFromRight)];

            break;

               default:

            break;

    }

    

    static int i = 1;

    if (i > 6) {

        i = 1;

    }

    [self addBackgroundImgWithName:[NSString stringWithFormat:@"0%d.jpg",i]];

    i += 1;

}

#pragma CATransition========动画实现=======

- (void)transitionWithType:(NSString*) type withSubtype:(NSString*)subType forView:(UIView*)view{

    //创建CATranstion对象

    CATransition *animation = [CATransition animation];

    //设置运动时间

    animation.duration = DURATION;

    //设置动画的类型type

    animation.type = type;

    if (subType != nil) {

        //设置子类

        animation.subtype = subType;

    }

    //设置动画出现的时候的运动速度

    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;

    [view.layer addAnimation:animation forKey:@"animation"];

}

#pragma mark ============UIView动画实现=============

- (void)animationWithView:(UIView*)view withAnimationTransition:(UIViewAnimationTransition)transtion{

    [UIView animateWithDuration:DURATION animations:^{

        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

        [UIView setAnimationTransition:transtion forView:view cache:YES];

    }];

}

 

- (void)addBackgroundImgWithName:(NSString*)imgeName{

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imgeName]];

}

代码放到github上,喜欢就给个Star:https://github.com/JakeTorres/Jake-sHouse

推荐阅读