Swift: 如何给带模糊背景的UIButton设置圆角?

5

我想制作一个带有轻微模糊效果和生动感背景的圆形UIButton。

到目前为止,我已经有了一个圆形的UIButton,但我在网上找到的代码(我是iOS开发的新手,不太理解模糊等如何工作)只能将模糊效果添加到整个按钮的框架中,从而使按钮再次呈现为正方形。

我还尝试添加一个视图,然后放置UIButton并添加模糊效果,并将cornerRadius代码应用于该视图,但也没有奏效。

这是我尝试过的:

    shortcutButton.layer.cornerRadius = 0.5 * shortcutButton.bounds.size.width // add the round corners in proportion to the button size

    let blur = UIVisualEffectView(effect: UIBlurEffect(style:
        UIBlurEffectStyle.Light))
    blur.frame = shortcutButton.bounds
    blur.userInteractionEnabled = false //This allows touches to forward to the button.
    shortcutButton.insertSubview(blur, atIndex: 0)
1个回答

8
在添加子视图之前,请将以下两行代码添加到您的项目中:
blur.layer.cornerRadius = 0.5 * shortcutButton.bounds.size.width
blur.clipsToBounds = true

祝您愉快! :)


太棒了!感谢你的帮助。我现在明白了 :) 能不能让模糊效果更“生动”一些? - Rs2845

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接