removeFromSuperview() 方法不可动画化,您可以做的是让 alpha 变为 0,完成后您可以安全地从超级视图中移除。
如果您想获得与推送相同的效果,您只需要获取您的代码并创建相反的转换即可。由于您似乎正在使用视图控制器,因此您可以利用转换 API。
func push(_ viewController: UIViewController, animated: Bool, completion: (()->())?) {
let oldVC = viewControllersStack.topItem()!
viewController.view.frame = oldVC.view.frame
self.addChildViewController(viewController)
oldVC.willMove(toParentViewController: nil)
let duration = animated ? Constants.GeneralValues.PopPushAnimationDuration : 0.0
transition(from: oldVC, to: viewController, duration: duration, options: [], animations: { () -> Void in
let animation = CATransition()
animation.duration = CFTimeInterval(duration)
animation.type = kCATransitionMoveIn
animation.timingFunction = CAMediaTimingFunction(name: "easeInEaseOut")
animation.subtype = "fromRight"
animation.fillMode = "forwards"
self.mainContainerView.layer.add(animation, forKey: "animoteKey")
// Constraint
guard let v = viewController.view else {
return
}
v.translatesAutoresizingMaskIntoConstraints = false
let hConstr = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v]|", options:[], metrics:nil, views:["v":v])
let vConstr = NSLayoutConstraint.constraints(withVisualFormat: "V:|[v]|", options:[], metrics:nil, views:["v":v])
let constrs: [NSLayoutConstraint] = [hConstr, vConstr].flatMap {$0}
NSLayoutConstraint.activate(constrs)
}) { (finished) -> Void in
oldVC.removeFromParentViewController()
self.viewControllersStack.push(viewController)
viewController.didMove(toParentViewController: self)
if let completion = completion {
completion()
}
}
}
func popViewController(_ animated: Bool, completion: (()->())?) {
let oldVC = viewControllersStack.topItem()!
let viewController = viewControllersStack.penultimate!
viewController.view.frame = oldVC.view.frame
self.addChildViewController(viewController)
oldVC.willMove(toParentViewController: nil)
let duration = animated ? Constants.GeneralValues.PopPushAnimationDuration : 0.0
transition(from: oldVC, to: viewController, duration: duration, options: [], animations: { () -> Void in
let animation = CATransition()
animation.duration = CFTimeInterval(duration)
animation.type = kCATransitionReveal
animation.timingFunction = CAMediaTimingFunction(name: "easeInEaseOut")
animation.subtype = "fromLeft"
animation.fillMode = "forwards"
self.mainContainerView.layer.add(animation, forKey: "animoteKey")
// Constraint
guard let v = viewController.view else {
return
}
v.translatesAutoresizingMaskIntoConstraints = false
let hConstr = NSLayoutConstraint.constraints(withVisualFormat: "H:|[v]|", options:[], metrics:nil, views:["v":v])
let vConstr = NSLayoutConstraint.constraints(withVisualFormat: "V:|[v]|", options:[], metrics:nil, views:["v":v])
let constrs: [NSLayoutConstraint] = [hConstr, vConstr].flatMap {$0}
NSLayoutConstraint.activate(constrs)
}) { (finished) -> Void in
print("Fine")
oldVC.removeFromParentViewController()
_ = self.viewControllersStack.pop()
viewController.didMove(toParentViewController: self)
if let completion = completion {
completion()
}
}
}
这是我用来实现您想要的相同的一种实现,但用于在容器中推送和弹出视图控制器,但动画是相同的,只是更改约束,以保持侧视图控制器薄。