如果您想设计自己的过渡,您可以为 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"];