自定义解散动画 [英] Custom unwind segue animation
问题描述
我进行了以下设置:
红色控制器具有方法
- (IBAction)unwindToRed:(UISegue *)segue
和a
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
会为此事件返回自定义序列。
which returns a custom segue for this event.
在我的自定义序列中,在执行代码中执行以下操作:
In my custom segue I have the following in the perform code:
- (void)perform
{
// Just helpers to get the controllers from the segue
UIViewController *destination = self.destinationViewController;
UIViewController *source = self.sourceViewController;
// Setting transitioning delegate for custom transitions
destination.transitioningDelegate = self;
source.transitioningDelegate = self;
destination.modalTransitionStyle = UIModalPresentationCustom;
source.modalPresentationStyle = UIModalPresentationCurrentContext;
// When I am transitioning to a new one
if(!self.reversed)
{
[source presentViewController:destination animated:YES completion:nil];
}
// Or reversing from a current one
else
{
[source dismissViewControllerAnimated:YES completion:nil];
}
}
您可以看到我已经设置了一个转换委托依次调用
As you can see I have setup a transition delegate which which in turn calls
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
在我应用自定义动画的动画控制器上。但是现在,当我在黄色视图上点击展开为红色时,它仅展开为其父级。 (例如蓝色的)。
on an animation controller where I apply my custom animation. But now when I tap Unwind to Red on the yellow view, it only "unwinds" to its parent. (e.g the blue one).
当我将自定义序列排除在等式之外时,它就很好了。它一直到顶部。
When I leave the custom segue out of the equation it works just fine. It goes al the way to the top.
现在我认为这与
[source dismissViewControllerAnimated:YES];
行,因为这通常只是向上,我的问题是我想,我应该怎么称呼在那里,需要时它会一直上升到红色?黄色是模式叠加层。因此, pop too root将无法工作。
line, since that usually just goes "one up" my question is I think, what should I call there so it goes all the way up to red when needed? Yellow is a modal overlay. So "pop too root" would not work.
推荐答案
指定自定义Segue
解决方案是使用自定义序列进行放松。 不幸的是,您无法像在常规序列中那样为界面构建器中的展开指定自定义序列。
UIViewController
上有一个可以覆盖的函数称为 segueForUnwindingToViewController
,它指定用于展开的序列。棘手的部分是知道哪个视图控制器将覆盖此功能。答案是在要与之转换的视图控制器的父级上执行此操作。在我的情况下,它是 UINavigationController
。导航控制器是第一视图控制器和第三视图控制器的容器视图控制器。如果我在视图中嵌入了子视图控制器,那么答案将不是导航控制器,而是父视图控制器。
There is a function on UIViewController
that you can override called segueForUnwindingToViewController
which specifies which segue to use for the unwind. The tricky part is knowing which view controller to override this function on. The answer is to do it on the parent of the view controllers you are transitioning to/from. In my case it is the UINavigationController
. The navigation controller is the container view controller for the first and third view controller. If I had embedded child view controllers in my views, then the answer would not be the navigation controller but the parent view controller.
通过继承 UINavigationController
。然后确保在界面构建器中设置导航控制器的定制类。现在,您可以覆盖 segueForUnwindingToViewController
函数。这是一个最小的实现。
Create a custom navigation controller by inheriting from UINavigationController
. Then make sure to set the custom class of your navigation controller in interface builder. Now you can override the segueForUnwindingToViewController
function. Here is a minimal implementation.
class MyNavigationController: UINavigationController {
override func segueForUnwindingToViewController(toViewController: UIViewController,
fromViewController: UIViewController,
identifier: String?) -> UIStoryboardSegue {
return UIStoryboardSegue(identifier: identifier, source: fromViewController, destination: toViewController)
}
}
向自定义Segue添加动画
Adding Animation to Custom Segue
override func segueForUnwindingToViewController(toViewController: UIViewController,
fromViewController: UIViewController,
identifier: String?) -> UIStoryboardSegue {
return UIStoryboardSegue(identifier: identifier, source: fromViewController, destination: toViewController) {
let fromView = fromViewController.view
let toView = toViewController.view
if let containerView = fromView.superview {
let initialFrame = fromView.frame
var offscreenRect = initialFrame
offscreenRect.origin.x -= CGRectGetWidth(initialFrame)
toView.frame = offscreenRect
containerView.addSubview(toView)
// Being explicit with the types NSTimeInterval and CGFloat are important
// otherwise the swift compiler will complain
let duration: NSTimeInterval = 1.0
let delay: NSTimeInterval = 0.0
let options = UIViewAnimationOptions.CurveEaseInOut
let damping: CGFloat = 0.5
let velocity: CGFloat = 4.0
UIView.animateWithDuration(duration, delay: delay, usingSpringWithDamping: damping,
initialSpringVelocity: velocity, options: options, animations: {
toView.frame = initialFrame
}, completion: { finished in
toView.removeFromSuperview()
if let navController = toViewController.navigationController {
navController.popToViewController(toViewController, animated: false)
}
})
}
}
}
详细信息为从这篇文章中
这篇关于自定义解散动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!