iPhone - 设计我自己的viewController转换

时间:2012-09-24 16:09:36

标签: iphone ios ipad viewcontroller

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

我找到了这个

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

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

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

我该怎么做?

感谢

1 个答案:

答案 0 :(得分:15)

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

这是你可以做界面的方法:

*。h file:

#import <UIKit/UIKit.h>

@interface UIViewController(Transitions)

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

@end

* .m file

#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"];