swift - 将模糊应用于透明弹出 View ,快速

标签 swift uiview popup blur

我有一个弹出 View 向用户显示一些信息,当前 View 的框架是一个黑色的半透明框。我想对此框应用一些模糊,以提高呈现给用户的内容的清晰度。

我有这个代码:

已更新

class PinDetails : UIView {

    var popupView:UIView!
    var txtName:UITextField!
    var img1:UIButton!
    var img2:UIButton!
    var img3:UIButton!
    var selectedIndex:Int!
    var dictLocation:[String:String]!

    var blur:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)

    override init(frame: CGRect) {

        super.init(frame: frame)

        var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
        effectView.frame = frame
        addSubview(effectView)

        popupView =  UIView(frame:TCRectMake(x: 25,y: 120,width: 270,height: 285))
        popupView.layer.cornerRadius = 20
        popupView.backgroundColor = UIColor(white:0, alpha: 0.5)
        self.addSubview(popupView)

我尝试实现的模糊代码似乎不起作用,xcode 指出: 实例成员“blur”不能用于“PinDetails”类型

感谢任何帮助,我对 Swift 还很陌生:)

最佳答案

尝试放置您的

var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
effectView.frame = frame
addSubview(effectView)

在 init() 内部。您正在使用尚未初始化的属性。

-- 更新 --

我建议使用您创建的 Popupview 框架来创建视觉效果 View 。在你的 init() 中尝试一下

    // Create the popup view
    popupView =  UIView(frame:CGRectMake(25,120,270,285))
    popupView.layer.cornerRadius = 20
    popupView.backgroundColor = UIColor(white:0, alpha: 0.5)

    // Create the Effect View and apply to Popup view
    let effect = UIVisualEffectView(frame: CGRectMake(0,0,popupView.layer.frame.width, popupView.layer.frame.height))
    effect.effect = blur
    popupView.addSubview(effect)

    // Add the popupview to the main view
    self.addSubview(popupView)

关于swift - 将模糊应用于透明弹出 View ,快速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38164516/

相关文章:

ios - Swift 中可测试的异步设计模式

ios - 点击 super View 中嵌入的自定义 UIView 中未收到的手势识别器

ipad - 无法使用 UIScrollView 时,如何在自定义 UIView 中模拟惯性滚动?

swift - 在表格 View 单元格中显示弹出窗口

javascript - FB 提要对话框不会在弹出窗口中显示图片缩略图

ios - 快速解析嵌套的 JSON 属性

swift - 如何为 UserDefaults 编写单元测试

ios - 创建在 UINavigationBarButton 中找到的缩进外观 - 以编程方式

javascript - 伪装重新加载为新的发布事件,父级不显示 "This page cannot be refreshed without resending information"消息

ios - Swift - 用于 Windows 身份验证的 NSURLSession