在我的应用程序中,我使用由平移手势触发的UIPercentDrivenInteractiveTransition来解雇viewController。我期待我的viewController被拖到右边,因为我正在平移它。然而,当我慢慢平移时,我得到了一个小故障:viewController从左向右快速跳跃。
这可以在这里看到:https://youtu.be/3IEtId1w7jM
以下是转换的代码:
class FilterHideTransition: UIPercentDrivenInteractiveTransition {
let viewController: FilterViewController
var enabled = false
private let panGesture = UIPanGestureRecognizer()
private let tapGesture = UITapGestureRecognizer()
init(viewController: FilterViewController) {
self.viewController = viewController
super.init()
panGesture.addTarget(self, action: #selector(didPan(with:)))
panGesture.cancelsTouchesInView = false
panGesture.delegate = self
tapGesture.addTarget(self, action: #selector(didTap(with:)))
tapGesture.cancelsTouchesInView = false
tapGesture.delegate = self
viewController.view.addGestureRecognizer(panGesture)
viewController.view.addGestureRecognizer(tapGesture)
}
}
//MARK: - Actions
private extension FilterHideTransition {
@objc func didPan(with recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: viewController.view)
let percentage = translation.x / viewController.view.frame.size.width
print(percentage)
switch recognizer.state {
case .began:
enabled = true
viewController.dismiss(animated: true, completion: nil)
break
case .changed:
update(percentage)
break
case .ended:
completionSpeed = 0.3
if percentage > 0.5 {
finish()
} else {
cancel()
}
enabled = false
break
case .cancelled:
cancel()
enabled = false
break
default:
cancel()
enabled = false
break
}
}
@objc func didTap(with recognizer: UITapGestureRecognizer) {
viewController.dismiss(animated: true, completion: nil)
}
func isTouch(touch: UITouch, in view: UIView) -> Bool {
let touchPoint = touch.location(in: view)
return view.hitTest(touchPoint, with: nil) != nil
}
}
extension FilterHideTransition: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if gestureRecognizer == tapGesture {
return !isTouch(touch: touch, in: viewController.panel)
} else if gestureRecognizer == panGesture {
return !isTouch(touch: touch, in: viewController.heightSlider) &&
!isTouch(touch: touch, in: viewController.widthSlider) &&
!isTouch(touch: touch, in: viewController.priceSlider)
} else {
return true
}
}
}
以下是动画师的代码:
class FilterHideAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.25
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? OverlayTabBarController
else { return }
let startFrame = fromVC.view.frame
let endFrame = CGRect(x: startFrame.size.width, y: 0, width: startFrame.size.width, height: startFrame.size.height)
UIView.animate(withDuration: transitionDuration(using: transitionContext),
delay: 0.0,
options: .curveEaseIn,
animations: {
fromVC.view.frame = endFrame
toVC.overlay.alpha = 0
},
completion: {
_ in
if transitionContext.transitionWasCancelled {
transitionContext.completeTransition(false)
} else {
transitionContext.completeTransition(true)
}
})
}
}
我的问题:如何防止这种故障发生?
答案 0 :(得分:9)
我测试了您的最小工作示例,并再次出现相同的问题。我无法使用UIView.animate
API对其进行修复,但如果您使用UIViewPropertyAnimator
则不会出现此问题 - 唯一的缺点是UIViewPropertyAnimator
仅适用于iOS 10+。
iOS 10+解决方案
第一个重构HideAnimator
来实现interruptibleAnimator(using:)
以返回执行过渡动画师的UIViewPropertyAnimator
对象(请注意,根据文档,我们应该返回相同的动画对象以进行正在进行的过渡) :
class HideAnimator: NSObject, UIViewControllerAnimatedTransitioning {
fileprivate var propertyAnimator: UIViewPropertyAnimator?
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.25
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// use animator to implement animateTransition
let animator = interruptibleAnimator(using: transitionContext)
animator.startAnimation()
}
func interruptibleAnimator(using transitionContext: UIViewControllerContextTransitioning) -> UIViewImplicitlyAnimating {
// as per documentation, we need to return existing animator
// for ongoing transition
if let propertyAnimator = propertyAnimator {
return propertyAnimator
}
guard let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
else { fatalError() }
let startFrame = fromVC.view.frame
let endFrame = CGRect(x: startFrame.size.width, y: 0, width: startFrame.size.width, height: startFrame.size.height)
let animator = UIViewPropertyAnimator(duration: transitionDuration(using: transitionContext), timingParameters: UICubicTimingParameters(animationCurve: .easeInOut))
animator.addAnimations {
fromVC.view.frame = endFrame
}
animator.addCompletion { (_) in
if transitionContext.transitionWasCancelled {
transitionContext.completeTransition(false)
} else {
transitionContext.completeTransition(true)
}
// reset animator because the current transition ended
self.propertyAnimator = nil
}
self.propertyAnimator = animator
return animator
}
}
使其成功的最后一件事,在didPan(with:)
删除以下行:
completionSpeed = 0.3
这将使用默认速度(1.0
,或者您可以明确设置)。使用interruptibleAnimator(using:)
时,将根据动画师的fractionComplete
自动计算完成速度。
答案 1 :(得分:3)
So the issue is actually that when you initiate an interactive transition, the animation tries to run in its entirety. If you put a breakpoint in the gestures change state, you can see the entire animation run, and when you resume, it picks back up tracking your finger. I tried a bunch of hacks around setting the interactive transition's progress to 0 but nothing seemed to work.
The solution involves setting the transition context's container view's layer speed to 0 during the transition and setting it back to 1 when the transition is ready to complete. I abstracted this code into a simple subclass of UIPercentDrivenInteractiveTransition
. The code looks something like:
@implementation InteractiveTransition {
id<UIViewControllerContextTransitioning> _transitionContext;
}
- (void)startInteractiveTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
_transitionContext = transitionContext;
[_transitionContext containerView].layer.speed = 0;
[super startInteractiveTransition:transitionContext];
}
- (void)finishInteractiveTransition {
[_transitionContext containerView].layer.speed = 1;
[super finishInteractiveTransition];
}
- (void)cancelInteractiveTransition {
[_transitionContext containerView].layer.speed = 1;
[super cancelInteractiveTransition];
}
@end
This will pause the animation until you're ready to finish or cancel the interactive transition.