我的应用程序有一堆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打这样的最终位置和东西创造状弹性弹跳的效果。 我喜欢我自己的关键帧添加到动画曲线。
我怎么做?
谢谢
如果你想设计自己的过渡,你可以创建一个类别的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"];