我使用UIViewControllerAnimatedTransitioning进行了自定义模式转换。转换完成后,模式显示的视图控制器将填充父视图的一部分,即父视图控制器的某些部分仍然可见。
我需要知道如何检测显示的视图控制器外部的点击(或部分显示的父视图控制器),这样我就可以用它来关闭模态。我理解如何向视图添加手势但我无法找到添加手势的视图。
我查看了stackoverflow,但没有专门针对自定义模式转换的示例。
以下是自定义转换的代码
class PartialSlideInAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
var direction: SlideAnimationDirectionStyle
var duration: TimeInterval = 0.125
var presentedControllerWidth: CGFloat = 250
let fadeAlpha: CGFloat = 0.5
init(direction: SlideAnimationDirectionStyle) {
self.direction = direction
super.init()
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return TimeInterval(duration)
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// Setup Views
guard let presentedFromVC = transitionContext.viewController(forKey: .from) else {transitionContext.completeTransition(false); return }
guard let presentingToVC = transitionContext.viewController(forKey: .to) else {transitionContext.completeTransition(false); return }
let contrainerView = transitionContext.containerView // アニメーションはこのViewで行われる、アニメーションを実装する presenter, presenteeをこの上に載せないとアニメーションが動かない
// Setup Frames
let positionHidden = CGRect(x: contrainerView.frame.width + 1, y: 0, width: presentedControllerWidth, height: contrainerView.frame.height)
let positionShown = CGRect(x: contrainerView.frame.width - presentedControllerWidth, y: 0, width: presentedControllerWidth, height: contrainerView.frame.height)
switch direction {
case .slideIn:
contrainerView.addSubview(presentingToVC.view)
presentingToVC.view.frame = positionHidden
UIView.animate(withDuration: duration, animations: {
presentingToVC.view.frame = positionShown
presentedFromVC.view.alpha = self.fadeAlpha
}, completion: { success in
transitionContext.completeTransition( success )
})
case .slideOut:
contrainerView.addSubview(presentedFromVC.view)
presentedFromVC.view.frame = positionShown
UIView.animate(withDuration: duration, animations: {
presentedFromVC.view.frame = positionHidden
presentingToVC.view.alpha = 1.0
}, completion: { success in
transitionContext.completeTransition( success )
})
}
}
}
答案 0 :(得分:2)
您是否尝试过使用UIGestureRecognizer
? UIGestureRecognizer可用于识别子视图控制器和父视图控制器内的敲击事件。对于swift 3,handleTapBehind
应该有UIRecognizerDelegate
函数,可以让您执行您正在寻找的内容。
查看记录的内容here
请参阅适用于纵向和横向的" Swift 3.1解决方案"在链接的帖子中。