【问题标题】:BlurView below a button with fading edgesBlurView 位于带有渐变边缘的按钮下方
【发布时间】:2016-06-10 10:16:08
【问题描述】:

我在我的 mapView 上创建了一个按钮。 为了使该按钮更明显,我在该按钮下方添加了一个模糊视图。 我不喜欢模糊视图的锐利边缘。 如何让模糊淡出慢慢过渡到 mapView?

编辑:更具体。我的意思是带有圆角的渐变模糊渐变。

【问题讨论】:

  • 你能把你用来添加按钮的代码放在地图上吗?
  • 我正在使用故事板
  • 你能画一张图片来展示你想要的按钮外观吗?根据您模糊的描述,很难在代码中绘制按钮

标签: ios swift view core-graphics blur


【解决方案1】:

我认为这可以帮助您,首先您需要子类化您的按钮并将此代码添加到 drawRect 并将 UIColor.blueColor().CGColor 替换为 yourColor.CGColor

class UICustomBackgroundButton: UIButton {

    override func draw(_ rect: CGRect) {
        // Drawing code
        super.draw(rect)
        let ctx = UIGraphicsGetCurrentContext();

        let path = CGPath(roundedRect: rect.insetBy(dx: self.frame.size.height/4, dy: self.frame.size.height/4) , cornerWidth: self.frame.size.height/8, cornerHeight: self.frame.size.height/8, transform: nil)

        ctx?.setShadow(offset: CGSize.zero, blur: self.frame.size.height/4, color: UIColor.blue.cgColor);
        ctx?.setFillColor(UIColor.blue.cgColor);

        //if number of cicles is our glow is darker
        for _ in 0..<6
        {
        ctx?.addPath(path);
        ctx?.fillPath();
        }
    }

}

我希望这对你有帮助。

这就是它的样子

【讨论】:

  • 我在尝试运行您的代码时遇到此错误:断言失败:(corner_height >= 0 && 2 *corner_height
  • @mike 我编辑了我的代码,这个错误是因为值是静态的,所以现在我的代码会根据你的按钮高度进行调整,希望对你有帮助!
【解决方案2】:

您是否尝试过cornerRadius 属性?那应该去除锋利的边缘

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-19
    • 2018-10-08
    • 1970-01-01
    • 2013-08-19
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多