2

我的应用程序有一堆 viewController,我正在尝试设计自己的动画过渡,从一个 viewController 到另一个。

我找到了这个

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

但这似乎不允许设计我自己的过渡曲线。

我想要的是在新的 viewControllers 到达最终位置时创建像弹性反弹之类的效果。我喜欢将自己的关键帧添加到动画曲线中。

我怎么做?

谢谢

4

1 回答 1

15

如果您想设计自己的过渡,您可以为 UIViewController 创建一个类别并自己编写动画。只有基本的动画已经可用。

这是你可以做的界面:

*.h 文件:

#import <UIKit/UIKit.h>

@interface UIViewController(Transitions)

- (void) presentViewController:(UIViewController *)viewController withPushDirection: (NSString *) direction;
- (void) dismissViewControllerWithPushDirection:(NSString *) direction;

@end

*.m 文件

#import "UIViewControllerWithTransitions.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIViewController(Transitions)

- (void) presentViewController:(UIViewController *)viewController withPushDirection: (NSString *) direction {

    [CATransaction begin];

    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.subtype = direction;
    transition.duration = 0.25f;
    transition.fillMode = kCAFillModeForwards;
    transition.removedOnCompletion = YES;

    [[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];        
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [CATransaction setCompletionBlock: ^ {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(transition.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
            [[UIApplication sharedApplication] endIgnoringInteractionEvents];        
        });
    }];

    [self presentViewController:viewController animated:NO completion:NULL];

    [CATransaction commit];

}

- (void) dismissViewControllerWithPushDirection:(NSString *) direction {

    [CATransaction begin];

    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.subtype = direction;
    transition.duration = 0.25f;
    transition.fillMode = kCAFillModeForwards;
    transition.removedOnCompletion = YES;

    [[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];        
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [CATransaction setCompletionBlock: ^ {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(transition.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
            [[UIApplication sharedApplication] endIgnoringInteractionEvents];        
        });
    }];

    [self dismissViewControllerAnimated:NO completion:NULL];

    [CATransaction commit];

}

@end

这是一个示例调用:

[self presentViewController: myVC withPushDirection:@"fromRight"]; 
于 2012-09-24T16:21:34.483 回答