CATransition在自定义Segue中
问题描述:
我想知道是否可以在自定义Segue转换中使用CATransition。我创建了我的CustomSegueTransition.m文件,并在我想要转换发生的自定义segue类中引用它。CATransition在自定义Segue中
这是我对CustomSegueTransition.m文件代码:
#import "CustomSegueTransition.h"
#import "QuartzCore/QuartzCore.h"
@implementation CustomSegueTransition
-(void) perform {
// set up an animation for the transition the content
CATransition *animation = [CATransition animation];
[animation setDuration:0.25];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
[UIView commitAnimations];
}
@end
它给了我没有错误警告,但是当按下按钮,以使SEGUE过渡的过渡不会执行。我是否错过了CATransition代码中的某些内容,或者这是不可能的?
谢谢!
答
@Grant
我没有完整的答案,但我没有,你永远不会调用一个新的观点因而不存在过渡通知。我不知道你是否正在拍摄的一推,流行,或模式,但这里是代码,你将需要添加类型:
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
[src presentModalViewController:dst animated:YES];
或
[src presentViewController:<#(UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>];
我相信还有其他设置自定义动画的问题,虽然我对解决方案感兴趣,但我没有答案。
答
不知道你在尝试什么,但我确实发现了一个问题。
您创建了一个过渡动画,但从未使用它。您需要将动画添加到要制作动画的视图的图层上。
如果你打算使用较低的杠杆CoreAnimation功能,你也不需要使用[UIView beginAnimation] ... [UIView commitAnimation]。
如果需要,请使用[CATransaction begin] ... [CATransaction commit]。
答
这里是工作
-(void)perform {
__block UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
__block UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
}
答
进口QuartzCore架构完整的代码。
- (void)pushViewWithCustomTranstion {
CATransition* transition = [CATransition animation];
transition.duration = 0.4f;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromRight;
[self.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
YourDestinataionController *yourVC = [[YourDestinataionController alloc] init];
[self.navigationController pushViewController:yourVC animated:NO];
}
很简单,使用自定义的过渡效果把你的视图控制器导航控制器最简单的代码。
'presentModalViewController:animated:'is deprecated,so'presentViewController:animated:completion:'should be used in this case – 2012-04-03 11:25:14