Han Pengbo
Han Pengbo

Reputation: 1436

CATransition not work

I want to make a custom segue with ripple transition.The transition works,but no ripple effect.Here is my code

    - (void)perform
{
    // Add your own animation code here.

    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.7;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = @"rippleEffect";

    [[[[self sourceViewController] view] layer] addAnimation:animation forKey:@"animation"];

    [[self sourceViewController] presentModalViewController:[self destinationViewController] animated:NO];


}

Upvotes: 0

Views: 738

Answers (2)

Delete
Delete

Reputation: 922

Here is a category method you can attach to UIViewController to use CAAnimations for transitions between child views in a parent view container. You may need to modify it for your purposes, but it shows how CATransitions are used properly for animations.

I wrote this to create a back and forth paging effect inside a parent UIViewController to swipe between child views.

 - (void) transitionFromView:(UIView*)fromView
                     toView:(UIView*)toView
         usingContainerView:(UIView*)container
              andTransition:(NSString*)transitionType{

    __block CGPoint targetOffset = fromView.center;
    __block BOOL transitionFromSameView = [toView isEqual:fromView];

    // In some cases, we want to perform the illusion of a transition when we are really just changing data in the same view.
    // In those cases, we don't need to perform this position modification.
    __block CGPoint centerOffset  = fromView.center;

    __block BOOL useAnimatedTransition = (transitionType != nil)?YES:NO;
    __block BOOL isLeftToRight = ([transitionType isEqualToString:kCATransitionFromRight])?YES:NO;

    if(transitionFromSameView == NO){
        CGFloat horizontalOffset = (isLeftToRight == YES)?[toView sizeWidth] + 100:-([toView sizeWidth] + 100);
        centerOffset = CGPointMake(fromView.center.x + horizontalOffset, fromView.center.y);
        [toView setCenter:centerOffset];
        [container insertSubview:toView belowSubview:fromView];
    }
    UIView *blockToView = toView;
    UIView *blockFromView = fromView;
    UIView *blockContainerView = container;

    if(useAnimatedTransition == NO){
        if(transitionFromSameView == NO){
            [blockToView setCenter:targetOffset];
            [blockFromView setCenter:centerOffset];
            [blockFromView removeFromSuperview];
        }  else {
            [blockToView setCenter:targetOffset];
            [blockFromView setCenter:centerOffset];
        }
    } else {

        [UIView animateWithDuration:kTransitionTime animations:^{

            CATransition *animation = [CATransition animation];
            [animation setDuration:kTransitionTime];
            [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
            [animation setType:kCATransitionMoveIn];
            [animation setSubtype:transitionType];
            [[blockContainerView layer] addAnimation:animation forKey:@"TransitionViews"];
            if(transitionFromSameView == NO){
                [blockToView setCenter:targetOffset];
                [blockFromView setCenter:centerOffset];
            }
        } completion:^(BOOL finished) {
            if(transitionFromSameView == NO){
                [blockFromView removeFromSuperview];
            }
        }];
    }
}

Upvotes: 0

jszumski
jszumski

Reputation: 7416

rippleEffect is an undocumented animation type and you can't rely on it working the same (or at all) between any particular iOS releases as discussed here: ripple effect animation. I would strongly suggest implementing the animation yourself or finding an alternative.

Upvotes: 1

Related Questions