[英]iOS - Creating a custom push segue animation
我正在嘗試創建一個push segue動畫,其中sourceVC垂直地從屏幕上掉落,而destinationVC則垂直向上顯示在屏幕上。 我的自定義segue似乎差不多制作這個動畫,但是,在目標VC出現動畫時不起作用。 這是我的-perform segue。
-(void)perform
{
UIViewController *sourceVC = self.sourceViewController;
UIViewController *destVC = self.destinationViewController;
[sourceVC.view addSubview:destVC.view];
float width = sourceVC.view.frame.size.width;
float height = sourceVC.view.frame.size.height;
destVC.view.frame = CGRectMake(0, 560, width, height);
[UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionCurveLinear
animations:^{
[destVC.view removeFromSuperview];
sourceVC.view.frame = CGRectMake(0, 560, width, height);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
destVC.view.frame = CGRectMake(0, 0, destVC.view.frame.size.width, destVC.view.frame.size.height);
} completion:^(BOOL finished) {
[sourceVC.navigationController pushViewController:destVC animated:NO];
}];
}];
}
第二個動畫是不起作用的動畫。
任何想法我的代碼有什么問題? 謝謝你的幫助。
我需要創建一個符合UIViewControllerAnimatedTransitioning的自定義Animator類。
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface SAAnimator : NSObject <UIViewControllerAnimatedTransitioning>
@end
然后我實現了所需的方法。
@implementation SAAnimator
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.2;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
fromViewController.view.userInteractionEnabled = NO;
[[transitionContext containerView]addSubview:fromViewController.view];
[[transitionContext containerView]addSubview:toViewController.view];
toViewController.view.frame = CGRectMake(0, 560, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
fromViewController.view.frame = CGRectMake(0, 560, fromViewController.view.frame.size.width, fromViewController.view.frame.size.height);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
toViewController.view.frame = CGRectMake(0, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}];
}
@end
然后很簡單,在我的視圖控制器中執行push segue,我使類符合UINavigationControllerDelegate並實現此方法。
#pragma mark - Animated Transiton
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if (operation == UINavigationControllerOperationPush) {
SAAnimator * animator = [SAAnimator new];
return animator;
}
return nil;
}
在prepareForSegue中設置導航控制器的委托
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"toCreateVC"]) {
self.navigationController.delegate = self;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.