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)
4

1 回答 1

8

在添加子视图之前,将以下两行代码添加到您的项目中:

blur.layer.cornerRadius = 0.5 * shortcutButton.bounds.size.width
blur.clipsToBounds = true

享受!:)

于 2016-01-02T20:10:45.323 回答