iOS - 创建自定义推送动画

时间:2015-04-13 20:37:42

标签: ios objective-c 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];
                         }];
                     }];
}

第二个动画是不起作用的动画。

任何想法我的代码有什么问题?谢谢你的帮助。

1 个答案:

答案 0 :(得分:3)

我需要创建一个符合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;
    }
}