gpt4 book ai didi

ios - 通过 UITapGestureRecognizer 移除 UIView 需要点击 2 次(快速)

转载 作者:行者123 更新时间:2023-11-28 07:58:51 26 4
gpt4 key购买 nike

我创建了一个 View ,该 View 在选择按钮时覆盖了整个屏幕。具体来说,我的 UICollectionViewCell 中有一个 UIButton,单击时,它会在设备的整个窗口上呈现覆盖。叠加层是一个 UIView 层次结构,其中 UIVisualEffectView 是叠加层的父 View 。当上述 UIButton 被选中时,UIVisualEffectView 作为 subview 添加到 ViewController.view 属性。

我还向 UIVisualEffectView 添加了一个 UITapGestureRecognizer。选择后,整个叠加层应该消失,通过从 ViewController.view subview 中移除。

唯一的问题是 UITapGestureRecognizer 的行为。我必须单击它两次才能使覆盖层消失。添加跟踪打印语句显示,在第一次单击时,UITapGestureRecognizer 操作 #selector 函数正在触发,但直到我再次单击它或单击模拟器框架(即移动模拟器)时,覆盖才会消失适本地。我怀疑这可能与线程和返回控制有关,但无法查明。下面是相关代码。任何帮助表示赞赏。

注意:我已经在 DispatchQueue.main.async() block 中有和没有 removeFromSuperview 语句的情况下运行它;无论有没有,行为都是一样的。

在 UIViewController 子类中设置:

var blurBackground = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.dark))

blurBackground.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(unblurView)))

目标 Action ,通过跟踪语句确认触发:

func unblurView() {
UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
print("In unblur func")
self.blurBackground.alpha = 0.0

DispatchQueue.main.async(execute: {
self.blurBackground.removeFromSuperview()
print("In unblur func - main thread removefromsuperview")
})

}, completion: nil)
}

如果有帮助,我可以添加与将覆盖 View 添加到 View Controller View 的 subview 相关的代码。

最佳答案

使用以下 View Controller 创建一个新的单 View 应用程序项目对我来说效果很好

class ViewController: UIViewController {

var blurBackground = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.dark))

override func viewDidLoad() {
super.viewDidLoad()
blurBackground.frame = self.view.frame
blurBackground.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ViewController.unblurView)))
view.addSubview(blurBackground)
}

@objc func unblurView() {
UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
print("In unblur func")
self.blurBackground.alpha = 0.0
DispatchQueue.main.async(execute: {
self.blurBackground.removeFromSuperview()
print("In unblur func - main thread removefromsuperview")
})
}, completion: nil)
}
}

如何将 blurBackground 添​​加到 View 中?

关于ios - 通过 UITapGestureRecognizer 移除 UIView 需要点击 2 次(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212421/

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