与之互动后,自定义UIButton的边缘看起来像素化了

时间:2019-07-01 14:32:59

标签: ios swift xcode uibutton antialiasing

我创建了一个带有圆角,渐变背景和阴影的自定义UIButton子类。启动应用程序后,按钮看起来可以正常显示并进行抗锯齿处理,但是如果我按下按钮,边缘就会变得像素化。

我已经尝试了很多方法,例如在按钮的图层上设置.allowsEdgeAntialiasing = true或从“突出显示”的设置器中删除比例转换动画等,根本没有帮助:(

这是我的按钮类:

@IBDesignable class CircleTintedButton: UIButton {
    @IBInspectable var cornerRadius : CGFloat = 1.0
    @IBInspectable var shadowOffsetWidth: CGFloat = 0.0
    @IBInspectable var shadowOffsetHeight: CGFloat = 2.0
    @IBInspectable var shadowColor : UIColor = UIColor.gray
    @IBInspectable var shadowOpacity: CGFloat = 0.3

    @IBInspectable var startColor: UIColor = .blue {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var endColor: UIColor = .green {
        didSet {
            setNeedsLayout()
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        layer.cornerRadius = cornerRadius
        layer.shadowColor = shadowColor.cgColor
        layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight)
        let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
        layer.shadowPath = shadowPath.cgPath
        layer.shadowOpacity = Float(shadowOpacity)

        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [startColor.cgColor, endColor.cgColor]        
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.6)

        gradientLayer.locations = [0, 1]
        gradientLayer.frame = bounds
        gradientLayer.cornerRadius = cornerRadius
        gradientLayer.masksToBounds = true

        layer.insertSublayer(gradientLayer, below: self.titleLabel?.layer)        
    }

    override var isHighlighted: Bool {
        get {
            return super.isHighlighted
        }
        set {
            let xScale : CGFloat = newValue ? 1.025 : 1.0
            let yScale : CGFloat = newValue ? 1.1 : 1.0
            UIView.animate(withDuration: 0.1) {
                let transformation = CGAffineTransform(scaleX: xScale, y: yScale)
                self.transform = transformation
            }

            super.isHighlighted = newValue
        }
    }
}

我的测试设备(iPhone 7 @ 12.1.2)上的一些屏幕截图:

启动应用后: https://vinishko.party/files/ok.jpg

按下此按钮后: https://vinishko.party/files/aliased.jpg

已经花了整整一天时间尝试解决此问题,请帮我:D 谢谢。

1 个答案:

答案 0 :(得分:0)

每次调用layoutSubviews()时,您要添加 另一个 渐变层。您可以通过为按钮添加@IBAction来确认这一点:

@IBAction func didTap(_ sender: Any) {

    if let b  = sender as? CircleTintedButton {
        let n = b.layer.sublayers?.count
        print("nunLayers: \(String(describing: n))")
    }

}

您会发现,每次点击,子层数都会增加。

将渐变图层添加为自定义按钮的var /属性,然后仅将其添加一次:

@IBDesignable class CircleTintedButton: UIButton {
    @IBInspectable var cornerRadius : CGFloat = 1.0
    @IBInspectable var shadowOffsetWidth: CGFloat = 0.0
    @IBInspectable var shadowOffsetHeight: CGFloat = 2.0
    @IBInspectable var shadowColor : UIColor = UIColor.gray
    @IBInspectable var shadowOpacity: CGFloat = 0.3

    // add this var / property
    private var gradLayer: CAGradientLayer?

    @IBInspectable var startColor: UIColor = .blue {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable var endColor: UIColor = .green {
        didSet {
            setNeedsLayout()
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        layer.cornerRadius = cornerRadius
        layer.shadowColor = shadowColor.cgColor
        layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight)
        let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
        layer.shadowPath = shadowPath.cgPath
        layer.shadowOpacity = Float(shadowOpacity)

        // only create / add the gradient layer once
        if gradLayer == nil {

            let gradientLayer = CAGradientLayer()

            gradientLayer.locations = [0, 1]
            gradientLayer.masksToBounds = true

            layer.insertSublayer(gradientLayer, below: self.titleLabel?.layer)

            self.gradLayer = gradientLayer

        }

        gradLayer?.colors = [startColor.cgColor, endColor.cgColor]
        gradLayer?.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradLayer?.endPoint = CGPoint(x: 1.0, y: 0.6)

        gradLayer?.frame = bounds
        gradLayer?.cornerRadius = cornerRadius

// original code
//      let gradientLayer = CAGradientLayer()
//      gradientLayer.colors = [startColor.cgColor, endColor.cgColor]
//      gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
//      gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.6)
//
//      gradientLayer.locations = [0, 1]
//      gradientLayer.frame = bounds
//      gradientLayer.cornerRadius = cornerRadius
//      gradientLayer.masksToBounds = true
//
//      layer.insertSublayer(gradientLayer, below: self.titleLabel?.layer)
    }

    override var isHighlighted: Bool {
        get {
            return super.isHighlighted
        }
        set {
            let xScale : CGFloat = newValue ? 1.025 : 1.0
            let yScale : CGFloat = newValue ? 1.1 : 1.0
            UIView.animate(withDuration: 0.1) {
                let transformation = CGAffineTransform(scaleX: xScale, y: yScale)
                self.transform = transformation
            }

            super.isHighlighted = newValue
        }
    }
}