gpt4 book ai didi

swift - 如何立即关闭我的 DimmingView 和 SlideMenu?

转载 作者:行者123 更新时间:2023-11-28 13:36:11 25 4
gpt4 key购买 nike

我创建了一个带有变暗 View 的幻灯片菜单。当用户在侧面菜单外点击时,我希望关闭幻灯片菜单和调光 View 。我怎样才能做到这一点?我已经在每个类(class)中创建了一个 TapRecognizer 并且它可以工作......但一次只能为每个类(class)。 Here是我的 MenuController,下面是我的 SlideTransition 和我的调光 View 。现在,当我在幻灯片菜单外单击时,它会删除调暗 View 。但它也应该删除幻灯片菜单。

class SlideinTransition: NSObject, UIViewControllerAnimatedTransitioning {

let menuViewController = MenuViewController()

var isPresenting = true
let dimmingView = UIView()

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.6
}

@objc func touchWasDetected() {
print("Touch detected")
dimmingView.removeFromSuperview()
menuViewController.dismiss(animated: true, completion: nil)
}

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.3
let finalHeight = toViewController.view.bounds.height

if isPresenting{

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(touchWasDetected))
dimmingView.addGestureRecognizer(tapGesture)

//adds the dimming view
dimmingView.backgroundColor = .black
dimmingView.alpha = 0.0
containerView.addSubview(dimmingView)
dimmingView.frame = containerView.bounds
//adds the menu view controller to our container
containerView.addSubview(toViewController.view)

//init frame off the screen
toViewController.view.frame = CGRect(x: -finalWidth, y: 0, width: finalWidth, height: finalHeight)
}

let transform = {
self.dimmingView.alpha = 0.5
toViewController.view.transform = CGAffineTransform(translationX: finalWidth, y: 0)
}

let identity = {
self.dimmingView.alpha = 0.0
fromViewController.view.transform = .identity
}

//animates the transition
let duration = transitionDuration(using: transitionContext)
let isCancelled = transitionContext.transitionWasCancelled
UIView.animate(withDuration: duration, animations: {
self.isPresenting ? transform() : identity()
}) { (_) in
transitionContext.completeTransition(!isCancelled)
}
}

当用户在侧边菜单之外/调光 View 上点击时,侧边菜单和调光 View 都应该被移除

最佳答案

我猜你正在使用 SlideinTransition 进行呈现和解散。因此,您应该将 dimmingView.removeFromSuperview()touchWasDetected() 移动到 UIView.animate 完成。

更新:我已经在示例项目上运行了这个,工作正常。

初始 Controller :

class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
let transition = SlideinTransition()

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segue.destination.transitioningDelegate = self
}

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
transition.isPresenting = true
return transition
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
transition.isPresenting = false
return transition
}
}

修改后的过渡:

extension UIViewController {
@IBAction func dismissControllerAnimated() {
dismiss(animated: true)
}
}

class SlideinTransition: NSObject, UIViewControllerAnimatedTransitioning {
var isPresenting = true
let dimmingView = UIView()

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.6
}

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.3
let finalHeight = toViewController.view.bounds.height

if isPresenting {
let tapGesture = UITapGestureRecognizer(target: toViewController, action: #selector(UIViewController.dismissControllerAnimated))
dimmingView.addGestureRecognizer(tapGesture)

//adds the dimming view
dimmingView.backgroundColor = .black
dimmingView.alpha = 0.0
containerView.addSubview(dimmingView)
dimmingView.frame = containerView.bounds
//adds the menu view controller to our container
containerView.addSubview(toViewController.view)

//init frame off the screen
toViewController.view.frame = CGRect(x: -finalWidth, y: 0, width: finalWidth, height: finalHeight)
}

let transform = {
self.dimmingView.alpha = 0.5
toViewController.view.transform = CGAffineTransform(translationX: finalWidth, y: 0)
}

let identity = {
self.dimmingView.alpha = 0.0
fromViewController.view.transform = .identity
}

//animates the transition
let duration = transitionDuration(using: transitionContext)
let isCancelled = transitionContext.transitionWasCancelled
UIView.animate(withDuration: duration, animations: {
self.isPresenting ? transform() : identity()
}) { (_) in
transitionContext.completeTransition(!isCancelled)
if !self.isPresenting {
self.dimmingView.removeFromSuperview()
}
}
}
}

关于swift - 如何立即关闭我的 DimmingView 和 SlideMenu?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56595441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com