Is there an alternative way to perform this animat

2019-05-07 19:24发布

I'm currently performing a curl up animation by doing the following:

CATransition *animation = [CATransition animation];
animation.type = @"pageCurl";
animation.subtype = kCATransitionFromTop;
animation.fillMode = kCAFillModeForwards;
animation.duration = 1;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[[self.view.window layer] addAnimation:animation forKey:nil];

This animation will simply perform a page curl animation but in the end you are left looking at the same view that you started out with. No REAL transition ever occurred.

Now using animation blocks I know you can do something to the effect of:

[UIView transitionFromView:self.view
                    toView:aNewView    // a view to transition to
                  duration:1 
                   options:UIViewAnimationTransitionCurlUp|UIViewAnimationOptionCurveEaseIn
                completion:NULL];

However, using animation blocks you are transitioning to a new view, aNewView, which is different from the CATransition animation above which reuses the same view (no real transition ever occurs). The problem with the animation block is that you have to actually create the new view to transition to which is cumbersome in my case because the view is rather complicated and I'd rather not create a UIView subclass.

Is there a way to perform the above CATransition animation using animation blocks while getting around the difficulties of having to rebuild a view or create a custom subclass?

1条回答
smile是对你的礼貌
2楼-- · 2019-05-07 19:26

Ok - so I figured it out for anyone who is interested. It's actually super simple. You can simply use the method: transitionWithView:duration:options:animations:completion:

For example:

[UIView transitionWithView:self.view
                  duration:1
                   options:UIViewAnimationOptionTransitionCurlUp|UIViewAnimationCurveEaseIn
                animations:^{ // do anything you want here } 
                completion:NULL];

That's it. This will show a transition animation back to the same view. You can make any changes you want to the new view (maybe display some new text, whatever...) in the animation block.

I hope this helps someone out down the line...

查看更多
登录 后发表回答