当使用dismissViewController关闭模式视图控制器时,可以选择提供完成块。popViewController是否有类似的等价物
完成参数非常方便。例如,我可以使用它来推迟从表视图中删除一行,直到模式不在屏幕上,让用户看到行动画。当从推送的视图控制器返回时,我希望有同样的机会。
我曾尝试将popViewController放在UIView动画块中,在那里我确实可以访问完成块。但是,这会对要弹出的视图产生一些不必要的副作用。
如果没有这样的方法,有哪些变通方法?
发布于 2014-12-01 21:56:14
我知道一个答案已经被接受了两年多,但这个答案是不完整的。
没有办法做到你想要的开箱即用
这在技术上是正确的,因为UINavigationController应用编程接口没有为此提供任何选项。但是,通过使用CoreAnimation框架,可以向底层动画添加完成块:
[CATransaction begin];
[CATransaction setCompletionBlock:^{
// handle completion here
}];
[self.navigationController popViewControllerAnimated:YES];
[CATransaction commit];一旦popViewControllerAnimated:使用的动画结束,就会调用完成块。此功能已从iOS 4开始可用。
发布于 2016-04-23 18:51:32
Swift 5版本-工作起来就像一个护身符。基于this answer
extension UINavigationController {
func pushViewController(viewController: UIViewController, animated: Bool, completion: @escaping () -> Void) {
pushViewController(viewController, animated: animated)
if animated, let coordinator = transitionCoordinator {
coordinator.animate(alongsideTransition: nil) { _ in
completion()
}
} else {
completion()
}
}
func popViewController(animated: Bool, completion: @escaping () -> Void) {
popViewController(animated: animated)
if animated, let coordinator = transitionCoordinator {
coordinator.animate(alongsideTransition: nil) { _ in
completion()
}
} else {
completion()
}
}
}发布于 2015-01-30 17:14:41
我做了一个带有@JorisKluivers answer扩展的Swift版本。
这将在push和pop的动画完成后调用完成闭包。
extension UINavigationController {
func popViewControllerWithHandler(completion: ()->()) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.popViewControllerAnimated(true)
CATransaction.commit()
}
func pushViewController(viewController: UIViewController, completion: ()->()) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.pushViewController(viewController, animated: true)
CATransaction.commit()
}
}https://stackoverflow.com/questions/12904410
复制相似问题