Custom Segue Animation
我正在尝试使用自定义转场来执行一种缩放动画。
执行转换时,sourceViewController 变黑,然后发生缩放。
还尝试将 pushViewController: 设置为完成块,但根本不执行转换。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
– (void)perform {
UIViewController *sourceViewController = (UIViewController *) self.sourceViewController; [destinationViewController.view setTransform:CGAffineTransformMakeScale(0.5,0.5)]; [UIView animateWithDuration:0.5 } |
我做错了什么?
感觉很笨拙,但是您可以尝试在动画之前添加destinationViewController.view 作为子视图,然后在动画完成后将其删除并在没有动画的情况下将其推回。解决了过渡前的黑屏,但可能并不完美,这取决于你想用导航栏做什么,但可能更接近:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[sourceViewController.view addSubview:destinationViewController.view]; [destinationViewController.view setFrame:sourceViewController.view.window.frame]; [destinationViewController.view setTransform:CGAffineTransformMakeScale(0.5,0.5)]; [destinationViewController.view setAlpha:1.0]; [UIView animateWithDuration:0.5 |
注意,从 iOS 7 开始,您将使用自定义过渡。有关详细信息,请参阅 WWDC 2013 使用视图控制器的自定义转换。
例如,如果尝试使用导航控制器进行自定义转换,第一个视图控制器会将自己指定为导航控制器的委托:
1
|
self.navigationController.delegate = self;
|
然后,它将分别为 push 和 pop 指定自定义动画:
1
2 3 4 5 6 7 8 9 10 11 |
– (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { if (operation == UINavigationControllerOperationPush) { return [[PushAnimator alloc] init]; } else { return [[PopAnimator alloc] init]; } } |
然后你显然会实现这些动画师:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
@interface PushAnimator : NSObject <UIViewControllerAnimatedTransitioning>
@end @implementation PushAnimator – (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext – (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext [[transitionContext containerView] addSubview:toViewController.view]; [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:0 animations:^{ @end |
和
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
@interface PopAnimator : NSObject <UIViewControllerAnimatedTransitioning>
@end @implementation PopAnimator – (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext – (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view]; [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:0 animations:^{ @end |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268697.html