我想知道是否可以在自定义Segue Transition中使用CATransition。我创建了我的CustomSegueTransition.m文件并在我的自定义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代码中的内容,或者这是不可能的?
谢谢!
答案 0 :(得分:1)
@Grant
我没有完整的答案,但我确实注意到你从未调用过新的视图,因此没有过渡。我不知道你是在拍摄推送,弹出还是模态,但这里是你需要添加的代码类型:
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
[src presentModalViewController:dst animated:YES];
或
[src presentViewController:<#(UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>];
我相信设置自定义动画还有其他问题,虽然我对解决方案感兴趣,但我没有答案。
答案 1 :(得分:0)
不确定你在尝试什么,但我确实发现了一个问题。
您创建了一个过渡动画,但从未使用它。您需要在要设置动画的视图图层上添加动画。
如果您打算使用较低杠杆的CoreAnimation功能,您也不需要使用[UIView beginAnimation] ... [UIView commitAnimation]。
如果需要,使用[CATransaction begin] ... [CATransaction commit]。
答案 2 :(得分:0)
这是完整的代码
-(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];
}
答案 3 :(得分:0)
导入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];
}
使用自定义转换效果在导航控制器中推送视图控制器的代码简单易用。