iPhone - 设计我自己的 viewController 过渡
Posted
技术标签:
【中文标题】iPhone - 设计我自己的 viewController 过渡【英文标题】:iPhone - designing my own viewController transition 【发布时间】:2012-09-24 16:09:35 【问题描述】:我的应用有一堆 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 到达最终位置时创建像弹性弹跳这样的效果。我喜欢将自己的关键帧添加到动画曲线中。
我该怎么做?
谢谢
【问题讨论】:
【参考方案1】:如果您想设计自己的过渡,您可以为 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"];
【讨论】:
谢谢。我已经验证它适用于关键帧动画。谢谢。 我还没有什么好的,我刚刚创建了一些简单的关键帧动画来测试这个答案是否有效。 它可以比这更容易完成,但答案是有效的。 您能否提供一个更简单的解决方案示例?以上是关于iPhone - 设计我自己的 viewController 过渡的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS 中使用 AFNetworking 从 REST 接口定期轮询/拉取