BlurView below a button with fading edges

南楼画角 提交于 2019-12-12 03:06:57

问题


I created a button on top of my mapView. In order to make that Button more visible I added a blur view below that button. I don't like the sharp edges of my blur view. How do I make the blur fade out slowly transitioning into the mapView?

EDIT: To be more specific. I mean a fading blurred gradient with round corners.


回答1:


I think this can help you, first you need subclass your button and add this code in drawRect and replace UIColor.blueColor().CGColor by 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();
        }
    }

}

I Hope this helps you.

and this is how it look




回答2:


Have you tried the cornerRadius property? That should remove the sharp edges



来源:https://stackoverflow.com/questions/37745673/blurview-below-a-button-with-fading-edges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!