我正在尝试使用自定义 segue 在 UIViewController 之间添加简单的推送动画。
(无需将我的 Controller 转换为使用 UINavigationController)
到目前为止找到的答案在使用导航 Controller 时可以正常工作,但在不使用导航 Controller 时会失败。 (我仍在阅读并尝试我在堆栈溢出上看到的其他答案)
我的自定义 segue.m 感谢 (ZHCustomSegue.m) Zakir,于 2012 年 7 月 5 日
#import "SFCustomSegue.h"
#import "QuartzCore/QuartzCore.h"
@implementation SFCustomSegue
-(void)perform {
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = 4.0;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[sourceViewController.view.layer addAnimation:transition forKey:kCATransition];
[sourceViewController presentViewController:[self destinationViewController] animated:NO completion:nil];
}
如果我按照 Zakir 的原始示例使用导航 Controller 并将最后两行替换为:
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
它有效......
Apple 的文档说(注意不是导航 Controller ,而是使用模态转场):
- (void)perform
{
// Add your own animation code here.
[[self sourceViewController] presentModalViewController:[self destinationViewController] animated:NO];
}
如果不工作,我就看不到动画。在 Zakir 的代码中,使用 4.0 秒,需要 4.0 秒,我就得到了我想要的动画。当然,如果我使用导航 Controller ,则默认为推送转换。在此代码中,我没有看到任何动画,并且需要 0 秒。
有人获得了自定义的segue动画来与UIViewControllers一起使用吗?
最佳答案
由于您正在调用 presentViewController
,源 View Controller 将保留下来。如果您确实想用目标 View Controller 替换源 View Controller ,以便释放源 View Controller ,您可以这样做:
static UIImageView *screenShotOfView(UIView *view)
{
// Create a snapshot for animation
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImageView *screenShot = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
return screenShot;
}
- (void)perform
{
UIViewController *source = (UIViewController *) self.sourceViewController;
UIViewController *destination = (UIViewController *) self.destinationViewController;
// Swap the snapshot out for the source view controller
UIWindow *window = source.view.window;
UIImageView *screenShot = screenShotOfView(source.view);
CGRect originalFrame = destination.view.frame;
BOOL animsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
{
window.rootViewController = destination;
[window addSubview:screenShot];
[source.view removeFromSuperview];
CGRect frame = destination.view.frame;
frame.origin.x += source.view.bounds.size.width;
destination.view.frame = frame;
}
[UIView setAnimationsEnabled:animsEnabled];
[UIView animateWithDuration:kAnimationDuration
animations:^{
destination.view.frame = originalFrame;
CGRect frame = screenShot.frame;
frame.origin.x -= screenShot.bounds.size.width;
screenShot.frame = frame;
}
completion:^(BOOL finished) {
[screenShot removeFromSuperview];
}];
}
关于ios - 无需 UINavigationController 即可推送 Segue 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17375441/