这是我当前关于如何显示视图控制器然后将其分成页面一半的代码。单击灰色部分时,我试图关闭视图控制器。我不知道是否可能。现在,我用手势关闭了视图控制器。但是我必须触摸白色部分。我确定有些用户会遇到麻烦。
这是我的代码:
import UIKit
class slide: NSObject,UIViewControllerAnimatedTransitioning{
var ispresenting = false
let dimmingview = UIView()
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let toviewcontroller = transitionContext.viewController(forKey: .to),
let fromViewcontroller = transitionContext.viewController(forKey: .from) else { return }
let containerview = transitionContext.containerView
let finalWidth = toviewcontroller.view.bounds.width * 0.8
let finalHeight = toviewcontroller.view.bounds.height
//dims menu background
if ispresenting {
//add dimining view
dimmingview.backgroundColor = .black
dimmingview.alpha = 0.0
containerview.addSubview(dimmingview)
dimmingview.frame = containerview.bounds
//add menu view controller to container
containerview.addSubview(toviewcontroller.view)
//init frame of the screen
toviewcontroller.view.frame = CGRect(x: -finalWidth, y: 0, width: finalWidth, height: finalHeight)
}
//animate onto screen
let transform = {
self.dimmingview.alpha = 0.7
toviewcontroller.view.transform = CGAffineTransform(translationX: finalWidth, y: 0)
}
let identity = {
self.dimmingview.alpha = 0.0
fromViewcontroller.view.transform = .identity
}
// Animation of the transition
let duration = transitionDuration(using: transitionContext)
let isCancelled = transitionContext.transitionWasCancelled
UIView.animate(withDuration: duration, animations: {
self.ispresenting ? transform() : identity()
}) { (_) in
transitionContext.completeTransition(!isCancelled)
}
}
}