草庐IT

ios - UIVisualEffectView 不工作

coder 2023-09-11 原文

我正在尝试使用以下代码为我的 View 添加模糊效果:

let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
    visualEffectView.frame = containerView.bounds
    containerView.addSubview(visualEffectView)
    containerView.userInteractionEnabled = true
    containerView.bringSubviewToFront(visualEffectView)
    visualEffectView.alpha = 1.0

但是,我没有看到任何变化。

更新

抱歉,我认为我在最初的问题中没有明确表达我的意图。我想要实现的是这样的:

我的 View 包含一个充满整个屏幕的 UIImageView。接下来,在屏幕底部,我有一个容器,它是一个 UIView,它包含一些按钮等。我想要做的是向 containerView 添加模糊效果,以便 后面的UIImageView是模糊的,containerView所在的地方。所以基本上我将 containerView 的 alpha 设置为 0.5,这意味着它是半透明的,我可以看到它后面的图像。我现在要做的是模糊 containerView 后面的图像。

当前效果:

代码:

extension UIButton{

func setImage(image: UIImage?, inFrame frame: CGRect?, forState state: UIControlState){
    self.setImage(image, forState: state)

    if let frame = frame{
        self.imageEdgeInsets = UIEdgeInsets(
            top: frame.minY - self.frame.minY,
            left: frame.minX - self.frame.minX,
            bottom: self.frame.maxY - frame.maxY,
            right: self.frame.maxX - frame.maxX
        )
    }
}

}



class SingleImageFeedView: UIViewController {

lazy var mainImageView : UIImageView = {
    let imageView = UIImageView()
    imageView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.width * 1.3)
    imageView.contentMode = UIViewContentMode.ScaleAspectFill
    imageView.backgroundColor = UIColor.clearColor()
    imageView.image = UIImage(named: "pizza")
    return imageView
}()

let containerView : UIView = {
    let this = UIView(frame: CGRectMake(0,0, 600,150))
    this.backgroundColor = UIColor.clearColor()
    this.layer.cornerRadius = 5
    this.layer.borderColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0).CGColor
    this.layer.borderWidth = 0.5
    return this
}()


let captionAndProfileImageContainerView : UIView = {
    let this = UIView()
    //this.backgroundColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 0.0)
    this.backgroundColor = UIColor.clearColor()
    return this
}()

let profilePicImageView : UIImageView = {
    let imageView = UIImageView()
    imageView.frame = CGRectMake(0, 0, 53, 53)
    imageView.backgroundColor = UIColor.clearColor()
    imageView.image = UIImage(named: "pizza")
    imageView.contentMode = UIViewContentMode.ScaleToFill
    return imageView
}()

let captionTextView: UITextView = {
    let textField = UITextView(frame: CGRectMake(0,0, 150, 100))
    textField.textColor = UIColor.whiteColor()
    textField.editable = false
    textField.text = "dwwdwwwwdwddwd"
    textField.backgroundColor = UIColor.clearColor()
    textField.font = UIFont.systemFontOfSize(12.0)
    return textField
}()

let dividerLineView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
    return view
}()

let commentAndLikesContainerView : UIView = {
   let view = UIView()
    //view.backgroundColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 0.0)
    view.backgroundColor = UIColor.clearColor()
    return view
}()

let commentLabel : UILabel = {
    let label = UILabel()
    label.text = "23"
    return label
}()

let likesLabel : UILabel = {
    let label = UILabel()
    label.text = "25"
    return label
}()

let voteUpButton : UIButton = {
    let button = UIButton(frame: CGRectMake(0,0,25,25))
    button.setImage((scaleImage((UIImage(named: "upvote"))!, toSize: CGSizeMake(25, 25))), inFrame: CGRectMake(0,0,25,25), forState: .Normal)
    button.imageView?.contentMode = UIViewContentMode.Center
    button.imageView?.clipsToBounds = true
    button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
    return button
}()

let voteDownButton : UIButton = {
    let button = UIButton(frame: CGRectMake(0,0,25,25))
    button.setImage((scaleImage((UIImage(named: "upvote"))!, toSize: CGSizeMake(25, 25))), inFrame: CGRectMake(0,0,25,25), forState: .Normal)
    button.imageView?.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
    button.imageView?.contentMode = UIViewContentMode.Center
    button.imageEdgeInsets = UIEdgeInsetsMake(6, 10, 10, 10)
    button.imageView?.clipsToBounds = true
    return button
}()


let commentButton : UIButton = {
    let button = UIButton(frame: CGRectMake(0,0,25,25))
    button.setImage((scaleImage((UIImage(named: "comment_feed_icon"))!, toSize: CGSizeMake(25, 25))), inFrame: CGRectMake(0,0,25,25), forState: .Normal)
    button.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
    button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
    button.imageView?.clipsToBounds = true
    return button
}()

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.clearColor()
    setupViews()
}

func setupViews() {
    self.captionAndProfileImageContainerView.addSubview(profilePicImageView)
    self.captionAndProfileImageContainerView.addSubview(captionTextView)

    self.commentAndLikesContainerView.addSubview(voteUpButton)
    self.commentAndLikesContainerView.addSubview(voteDownButton)
    self.commentAndLikesContainerView.addSubview(commentButton)
    self.commentAndLikesContainerView.addSubview(commentLabel)
    self.commentAndLikesContainerView.addSubview(likesLabel)

    self.containerView.addSubview(captionAndProfileImageContainerView)
    self.containerView.addSubview(dividerLineView)
    self.containerView.addSubview(commentAndLikesContainerView)




    self.view.addSubview(containerView)
    self.view.bringSubviewToFront(containerView)
    self.view.addSubview(mainImageView)
    self.containerView.bringSubviewToFront(captionAndProfileImageContainerView)
    self.containerView.bringSubviewToFront(dividerLineView)
    self.containerView.bringSubviewToFront(commentAndLikesContainerView)


    profilePicImageView.translatesAutoresizingMaskIntoConstraints = false
    captionTextView.translatesAutoresizingMaskIntoConstraints = false
    voteDownButton.translatesAutoresizingMaskIntoConstraints = false
    containerView.translatesAutoresizingMaskIntoConstraints = false
    voteUpButton.translatesAutoresizingMaskIntoConstraints = false
    commentLabel.translatesAutoresizingMaskIntoConstraints = false
    commentButton.translatesAutoresizingMaskIntoConstraints = false
    commentAndLikesContainerView.translatesAutoresizingMaskIntoConstraints = false
    captionAndProfileImageContainerView.translatesAutoresizingMaskIntoConstraints = false
    likesLabel.translatesAutoresizingMaskIntoConstraints = false
    dividerLineView.translatesAutoresizingMaskIntoConstraints = false
    mainImageView.translatesAutoresizingMaskIntoConstraints = false


    //main imageview constraints
    self.view.addConstraint(NSLayoutConstraint(item: mainImageView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 35))
    self.view.addConstraint(NSLayoutConstraint(item: mainImageView, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: mainImageView, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: mainImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: mainImageView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: 0))
    self.view.sendSubviewToBack(mainImageView)


    //containerview constraints

    self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: -10))
    self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1, constant: 10))
    self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 1, constant: -10))
    self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: -160))

    //caption and profilepic constraints

    self.view.addConstraint(NSLayoutConstraint(item: captionAndProfileImageContainerView, attribute: .Top, relatedBy: .Equal, toItem: containerView, attribute: .Top, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: captionAndProfileImageContainerView, attribute: .Leading, relatedBy: .Equal, toItem: containerView, attribute: .Leading, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: captionAndProfileImageContainerView, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: captionAndProfileImageContainerView, attribute: .Bottom, relatedBy: .Equal, toItem: containerView, attribute: .Bottom, multiplier: 1, constant: -50))

    self.view.addConstraintsWithFormat("H:|-8-[v0(50)]", views: profilePicImageView)
    self.view.addConstraintsWithFormat("V:|-35-[v0(50)]", views: profilePicImageView)
    profilePicImageView.layer.cornerRadius = 25
    profilePicImageView.layer.masksToBounds = true



    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Top, relatedBy: .Equal, toItem: captionAndProfileImageContainerView, attribute: .Top, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Leading, relatedBy: .Equal, toItem: profilePicImageView, attribute: .Trailing, multiplier: 1, constant: 10))
    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Trailing, relatedBy: .Equal, toItem: captionAndProfileImageContainerView, attribute: .Trailing, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: captionTextView, attribute: .Bottom, relatedBy: .Equal, toItem: captionAndProfileImageContainerView, attribute: .Bottom, multiplier: 1, constant: 0))

    //likes and comments and divider


    self.view.addConstraint(NSLayoutConstraint(item: commentAndLikesContainerView, attribute: .Top, relatedBy: .Equal, toItem: captionAndProfileImageContainerView, attribute: .Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: commentAndLikesContainerView, attribute: .Leading, relatedBy: .Equal, toItem: containerView, attribute: .Leading, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: commentAndLikesContainerView, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: commentAndLikesContainerView, attribute: .Bottom, relatedBy: .Equal, toItem: containerView, attribute: .Bottom, multiplier: 1, constant: 0))


    self.view.addConstraint(NSLayoutConstraint(item: voteUpButton, attribute: .Top, relatedBy: .Equal, toItem: commentAndLikesContainerView, attribute: .Top, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: voteUpButton, attribute: .Leading, relatedBy: .Equal, toItem: commentAndLikesContainerView, attribute: .Leading, multiplier: 1, constant: 10))


    self.view.addConstraint(NSLayoutConstraint(item: likesLabel, attribute: .Top, relatedBy: .Equal, toItem: commentAndLikesContainerView, attribute: .Top, multiplier: 1, constant: 10))
    self.view.addConstraint(NSLayoutConstraint(item: likesLabel, attribute: .Leading, relatedBy: .Equal, toItem: voteUpButton, attribute: .Trailing, multiplier: 1, constant: 10))

    self.view.addConstraint(NSLayoutConstraint(item: voteDownButton, attribute: .Top, relatedBy: .Equal, toItem: commentAndLikesContainerView, attribute: .Top, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: voteDownButton, attribute: .Leading, relatedBy: .Equal, toItem: likesLabel, attribute: .Trailing, multiplier: 1, constant: 10))

    self.view.addConstraint(NSLayoutConstraint(item: commentButton, attribute: .Top, relatedBy: .Equal, toItem: commentAndLikesContainerView, attribute: .Top, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: commentButton, attribute: .Leading, relatedBy: .Equal, toItem: voteDownButton, attribute: .Trailing, multiplier: 1, constant: 30))

    self.view.addConstraint(NSLayoutConstraint(item: commentLabel, attribute: .Top, relatedBy: .Equal, toItem: commentAndLikesContainerView, attribute: .Top, multiplier: 1, constant: 10))
    self.view.addConstraint(NSLayoutConstraint(item: commentLabel, attribute: .Leading, relatedBy: .Equal, toItem: commentButton, attribute: .Trailing, multiplier: 1, constant: 10))



    if !UIAccessibilityIsReduceTransparencyEnabled() {
        self.containerView.backgroundColor = UIColor.clearColor()

        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        //always fill the view
        blurEffectView.frame = containerView.bounds
        blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

        self.containerView.insertSubview(blurEffectView, atIndex: 1) //if you have more UIViews, use an insertSubview API to place it where needed


    }
    else {
        self.view.backgroundColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 0.5)
    }



    containerView.clipsToBounds = true

}


}

最佳答案

我用一个 UIImageView 和一个容器做了一个简单的项目。将模糊应用到容器会模糊下面的照片。 Storyboard中的容器背景设置为透明。

class ViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()

    let containerView : UIView = {
        let this = UIView(frame: CGRectMake(100,200, 200,200))
        this.backgroundColor = UIColor.clearColor()
        this.layer.cornerRadius = 5
        this.layer.borderColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0).CGColor
        this.layer.borderWidth = 0.5
        return this
    }()

    let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))

    visualEffectView.frame = containerView.bounds

    self.view.addSubview(containerView)
    containerView.insertSubview(visualEffectView, atIndex: 0)
    let secondImg = UIImageView(image: UIImage(named: "swift_tut.png"))

    secondImg.frame = CGRectMake(150,250, 200,200)

    self.view.addSubview(secondImg)
    self.view.bringSubviewToFront(containerView)





    // Do any additional setup after loading the view, typically from a nib.
}

关于ios - UIVisualEffectView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37860740/

有关ios - UIVisualEffectView 不工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  4. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  5. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  6. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  7. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  8. ruby - JetBrains RubyMine 3.2.4 调试器不工作 - 2

    使用Ruby1.9.2运行IDE提示说需要gemruby​​-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall

  9. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  10. ruby - `rescue $!` 是如何工作的? - 2

    我知道全局变量$!包含最新的异常对象,但我对下面的语法感到困惑。谁能帮助我理解以下语法?rescue$! 最佳答案 此构造可防止异常停止您的程序并使堆栈跟踪冒泡。它还会将该异常作为值返回,这很有用。a=get_me_datarescue$!在此行之后,a将保存请求的数据或异常。然后您可以分析该异常并采取相应措施。defget_me_dataraise'Nodataforyou'enda=get_me_datarescue$!puts"Executioncarrieson"pa#>>Executioncarrieson#>>#更现实的

随机推荐