[英]Hide a black fade when self.dismiss with CATransition
I want to make my viewController to dismiss to an old viewController with animation. 我想让我的viewController转移到带有动画的旧viewController上。 But when I use CATransition my viewController and old viewController fades black.
但是当我使用CATransition时,viewController和旧的viewController会变黑。 Is there any way I can make it not to fade?
有什么办法可以使它不褪色?
let transition = CATransition()
transition.duration = 1.0
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
view.window!.layer.add(transition, forKey: kCATransition)
self.dismiss(animated: false, completion: nil)
Read more about transition here and try to modify available parameters to achieve required effects. 在此处阅读有关过渡的更多信息,并尝试修改可用参数以达到所需的效果。
Good undocumented info about transition available here . 此处提供有关过渡的良好无证信息。
In case you want some good solution - you should prepare custom transition for your screen - check this WWDC video 如果您需要一些好的解决方案-您应该为屏幕准备自定义过渡-查看此WWDC视频
Alternative fast (but not the best) solution may be manual control of presentation/dismiss in your viewController: 另一种快速(但不是最好的)解决方案可能是在viewController中手动控制显示/关闭:
import UIKit
final class AnimatableViewController: UIViewController {
private var panStartLocation:CGPoint = CGPoint.zero
private var panPrevLocation:CGPoint = CGPoint.zero
var onClose:(()->())?
// MARK: - LifeCycle
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
openAnimation()
}
// MARK: - IBAction
@objc private func closeButtonAction(_ sender: UIButton) {
dismiss(animated: true, completion: { [weak self] in
self?.onClose?()
})
}
// MARK: - Gestures
@IBAction private func didDetectPan(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
panStartLocation = sender.location(in: navigationController?.view)
panPrevLocation = panStartLocation
case .cancelled, .ended:
finishAnimation()
case .changed:
let newPoint:CGPoint = sender.location(in: navigationController?.view)
let dy:CGFloat = newPoint.y - panPrevLocation.y
var newPosition:CGPoint = view.center
if (newPoint.y - panStartLocation.y > 0) {
newPosition.y += dy
} else {
newPosition.y = self.view.bounds.height / 2
}
view.center = newPosition
panPrevLocation = newPoint
default:
break
}
}
//MARK: Animation
private func finishAnimation() {
let center:CGPoint = view.center
if (center.y > self.view.bounds.height / 2 * 1.35) {
closeAnimation()
} else {
openAnimation()
}
}
private func closeAnimation() {
let fullAnimTime:CGFloat = 0.5
let endPoint:CGPoint = CGPoint(x:view.center.x, y:view.bounds.height / 2 * 3)
let animTime:CGFloat = ((endPoint.y - view.center.y) / self.view.bounds.height) * fullAnimTime
UIView.animate(withDuration: TimeInterval(animTime), animations: { [weak self] in
self?.view.center = endPoint
}, completion: { [weak self] _ in
self?.dismiss(animated: false, completion: {
self?.onClose?()
})
})
}
private func openAnimation() {
let fullAnimTime:CGFloat = 0.5
let endPoint:CGPoint = CGPoint(x: view.center.x, y:self.view.bounds.height / 2)
let animTime:CGFloat = ((view.center.y - endPoint.y) / (self.view.bounds.height / 2)) * fullAnimTime
UIView.animate(withDuration: TimeInterval(animTime)) { [weak self] in
self?.view.center = endPoint
}
}
}
extension AnimatableViewController: UIGestureRecognizerDelegate {
// MARK: - UIGestureRecognizerDelegate
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer is UIPanGestureRecognizer && otherGestureRecognizer is UIPanGestureRecognizer {
return false
}
return true
}
}
In storyboard don't forget to set you segue parameters as: 在情节提要中,不要忘记将segue参数设置为:
And add panGesture to your root view or any additional pan Gesture to top most view, that can be used for pan - in my case - i just add pan to rootView: 并将panGesture添加到您的根视图中,或将任何其他pan Gesture添加到最顶视图中,这些可用于平移-在我的情况下-我只是将pan添加到rootView中:
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.