- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 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/
我正在做一个项目,我的 android 在这个项目中作为一个网络服务器工作;输入带端口号的 IP 地址,打开 Web 界面,用户可以将文件上传到手机。我想在 Web 界面上显示一些图片,以便我们的界面
我是一名优秀的程序员,十分优秀!