【问题标题】:Setting a UIImageView to an initial state of blurred将 UIImageView 设置为模糊的初始状态
【发布时间】: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


    【解决方案1】:

    我得到了以下代码,可以按照您的意愿工作。我更改了 toggleBlurView 函数,让它更灵活一些,但这不应该影响功能。

    //
    //  ViewController.swift
    //  tableviewheader-test
    //
    //  Created by Forest Kunecke on 3/29/17.
    //  Use this code for freeeeee!!!!  Free as in free beer!!
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var myImage: UIImageView!
        var blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            toggleBlurView(myImage)
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
        @IBAction func toggleBlur(_ sender: Any) {
            toggleBlurView(myImage)
        }
    
        func toggleBlurView(_ imageView: UIImageView) {
            // if there's a blurView, remove it...
            if blurView.isDescendant(of: imageView) {
                blurView.removeFromSuperview()
            }
                // otherwise, add blurView to backgroundImage
            else {
                blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
                blurView.frame = imageView.bounds
                imageView.addSubview(blurView)
            }
        }
    
    }
    

    这是我的故事板的截图:

    【讨论】:

    • 太棒了!不知道为什么原始代码不起作用,但我喜欢您将图像指定为参数的方法。我仍然无法在viewDidLoad 中正确显示模糊,但我确实让它在viewDidAppear 中工作。感谢您的解决方案。
    • @Adrian From x-code 8 UIViewController 的生命周期有很多不同的效果。它只会加载您的UIVC,但不会正确加载您的UIView。为确认打印在viewDidLoadviewDidAppear 中的任何视图并检查差异。在您的情况下,视图可能无法正确识别并且视图不会变得模糊。
    猜你喜欢
    • 2020-11-02
    • 1970-01-01
    • 2020-06-16
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多