【发布时间】:2017-03-30 03:14:28
【问题描述】:
我最初尝试模糊 UIViewController 上的背景图像。当调用@IBAction 时,我想取消模糊backgroundImage。我可以让图像最初模糊,但如果我最初模糊它,我无法摆脱模糊。但是,如果我最初不模糊 backgroundImage,我的方法会根据需要在模糊图像和未模糊图像之间切换。
这些是我的UIViewController 类中的相关变量。
@IBOutlet weak var backgroundImage: UIImageView!
// I also leave this hanging around so I can add/remove it as needed
var blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
当视图加载时,我最初希望backgroundImage 有一个UIVisualEffectView 覆盖它。我最初尝试在viewDidLoad 中调用toggleBlurView,但UIBlurEffect 的显示已关闭,因此我在viewDidLayoutSubviews 中添加了toggleBlurView 的代码,从而解决了该问题。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// if I comment this out, the toggleBlurView() method works as desired
toggleBlurView()
}
这是我的toggleBlurView 方法。
func toggleBlurView() {
// if there's a blurView, remove it...
if blurView.isDescendant(of: backgroundImage) {
blurView.removeFromSuperview()
}
// otherwise, add blurView to backgroundImage
else {
blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
blurView.frame = backgroundImage.bounds
backgroundImage.addSubview(blurView)
}
}
我有一个@IBAction,它在被调用时也会运行toggleBlurView 方法。
@IBAction func playFrequency(_ sender: UIButton) {
// do stuff
toggleBlurView()
}
我的代码没有崩溃,但我无法得到想要的结果。我不确定我是否发现了错误或我做错了什么。我赌的是后者。感谢您的阅读,欢迎提出如何将图像设置为初始模糊状态的建议。
【问题讨论】:
标签: ios swift uiimageview uivisualeffectview uiblureffect