Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use UIView animation to implement kCATransitionPush from CATransition

Apple recommended to use UIView animation blocks for iOS4 and later (here), but I liked my old CATransition with kCATransitionPush effect very much. How can I implement kCATransitionPush with UIView animation? I only see four types of view transition in the available options (flip left/right, curl up/down)

If I implement by myself, I am planning to create a new view and slide it in while sliding the old view out. I don't understand why apple doesn't include the "push" effect in UIView animation.

Thanks!

leo

like image 368
leo Avatar asked Jul 24 '11 07:07

leo


4 Answers

From reading the apple docs that you posted, it also appears to say:

The block-based animation methods (such as animateWithDuration:animations:) greatly simplify the creation of animations. With one method call, you specify the animations to be performed and the options for the animation. However, block-based animations are available only in iOS 4 and later. If your application runs on earlier versions of iOS, you must use the beginAnimations:context: and commitAnimations class methods to mark the beginning and ending of your animations.

I take that to mean they reccomend it because it greatly simplifies the creation of animations. It also notes that it wont work with any devices not having ios 4. If you know how to use CAAnimation, I'd keep using it. I still use this without any issues:

- (void)switchTwoViews:(UIView *)view1 otherView:(UIView *)view2 direction:(int)directionRL{
view2.frame = CGRectMake(0, 0, 400, 211);
visibleView = view2;
// remove the current view and replace with view1
[view1 removeFromSuperview];
[currentOptionsView addSubview:view2];

// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.3];
[animation setType:kCATransitionPush];
if (directionRL == 0) {
    [animation setSubtype:kCATransitionFromRight];
} else {
    [animation setSubtype:kCATransitionFromLeft];
}

[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[currentOptionsView layer] addAnimation:animation forKey:@"SwitchToView"];

}

like image 160
Daniel G. Wilson Avatar answered Nov 17 '22 07:11

Daniel G. Wilson


The documentation is referring to recommending block based UIView animations over using the older UIView calls of beginAnimations and commitAnimations. It has nothing to do with CATransition. You can carry on using Core Animation without any worries.

like image 45
Benjamin Mayo Avatar answered Nov 17 '22 05:11

Benjamin Mayo


Try below Code for Showing the View

-(void)slideViewTransition:(UIView*)slidingView
{ 
  CATransition *animation = [CATransition animation];
 [animation setDuration:0.4];
 [animation setType:kCATransitionPush];
 [animation setSubtype:kCATransitionFromTop];
 [animation setTimingFunction:[CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

 [[slidingView layer] addAnimation:animation forKey:nil];

}

When you need to hide the View just change the [animation setSubtype:kCATransitionFromTop] to [animation setSubtype:kCATransitionFromBottom];

I hope it may help you..!!!!

like image 1
Kamar Shad Avatar answered Nov 17 '22 05:11

Kamar Shad


Here a Swift function that animates a view from left/right according to the given flag :

func animateSwipe(on view: UIView, left: Bool) {
    let transition = CATransition()
    transition.duration = 0.45
    transition.type = kCATransitionPush
    transition.subtype = left ? kCATransitionFromLeft : kCATransitionFromRight
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
    view.layer.add(transition, forKey: nil)
    // change anything you want on the view in a reload(view: UIView) function
    reload(view)
}
like image 1
dulgan Avatar answered Nov 17 '22 05:11

dulgan