Reputation: 1011
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
Upvotes: 5
Views: 7618
Reputation: 6694
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)
}
Upvotes: 1
Reputation: 6067
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..!!!!
Upvotes: 1
Reputation: 6679
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.
Upvotes: 2
Reputation: 15065
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"];
}
Upvotes: 4