尝试对uibezierPath进行子类化时,编码错误中缺少参数的参数

问题描述

我希望我的快速代码显示一个uibezierPath按钮。该代码使用重写功能绘制来绘制按钮。代码出现编译错误。它告诉我我在let customButton = FunkyButton(coder:)中缺少参数,可以在NSCODER中看到错误。我不知道要为nscoder放置什么。你觉得我应该放什么?

import UIKit

class ViewController: UIViewController {
    var Box = UIImageView()
    override open var shouldAutorotate: Bool {
        return false
    }
    
    // Specify the orientation.
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeRight
    }
    
    let customButton = FunkyButton(coder: <#NSCoder#>)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(Box)
        
//        Box.frame = CGRect(x: view.frame.width * 0.2,y: view.frame.height * 0.2,width: view.frame.width * 0.2,height: view.frame.height * 0.2)
        
        Box.backgroundColor = .systemteal
        customButton!.backgroundColor = .systemPink
        
        
        self.view.addSubview(customButton!)

        customButton?.addTarget(self,action: #selector(press),for: .touchDown)
    }
    
    @objc func press(){
        print("hit")
    }
    
}

class FunkyButton: UIButton {
  var shapeLayer = CAShapeLayer()
 let aPath = UIBezierPath()
    override func draw(_ rect: CGRect) {
       let aPath = UIBezierPath()
        aPath.move(to: CGPoint(x: rect.width * 0.2,y: rect.height * 0.8))
        aPath.addLine(to: CGPoint(x: rect.width * 0.4,y: rect.height * 0.2))


        //design path in layer
        shapeLayer.path = aPath.cgPath
        shapeLayer.strokeColor = UIColor.red.cgColor
        shapeLayer.linewidth = 1.0

        shapeLayer.path = aPath.cgPath

        // draw is called multiple times so you need to remove the old layer before adding the new one
        shapeLayer.removeFromSuperlayer()
        layer.addSublayer(shapeLayer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func hitTest(_ point: CGPoint,with event: UIEvent?) -> UIView? {
        if self.isHidden == true || self.alpha < 0.1 || self.isUserInteractionEnabled == false {
            return nil
        }
        if aPath.contains(point) {
            return self
        }
        return nil
    }
}

解决方法

实例化FunkyButton时,请勿手动调用coder格式。只需致电

let button = FunkyButton()

或将其添加到IB并连接插座

@IBOutlet weak var button: FunkyButton!

FunkyButton中,您不应该在draw(_:)方法内更新形状层路径。在初始化期间,只需将形状图层添加到图层层次结构中,每当您更新形状图层的路径时,都会为您呈现。不需要/ draw(_:)

@IBDesignable
class FunkyButton: UIButton {
    private let shapeLayer = CAShapeLayer()
    private var path = UIBezierPath()

    // called if button is instantiated programmatically (or as a designable)

    override init(frame: CGRect = .zero) {
        super.init(frame: frame)
        configure()
    }

    // called if button is instantiated via IB

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure()
    }

    // called when the button’s frame is set

    override func layoutSubviews() {
        super.layoutSubviews()
        updatePath()
    }

    override func hitTest(_ point: CGPoint,with event: UIEvent?) -> UIView? {
        guard path.contains(point) else {
            return nil
        }

        return super.hitTest(point,with: event)
    }
}

private extension FunkyButton {
    func configure() {
        shapeLayer.strokeColor = UIColor.red.cgColor
        shapeLayer.lineWidth = 1

        layer.addSublayer(shapeLayer)
    }

    func updatePath() {
        path = UIBezierPath()
        path.move(to: CGPoint(x: bounds.width * 0.2,y: bounds.height * 0.8))
        path.addLine(to: CGPoint(x: bounds.width * 0.4,y: bounds.height * 0.2))
        path.addLine(to: CGPoint(x: bounds.width * 0.2,y: bounds.height * 0.2))
        path.close()
        
        shapeLayer.path = path.cgPath
    }
}

如果您真的想在draw(_:)中绘制路径,这也是可以接受的模式,但是您根本不会使用CAShapeLayer,只需手动stroke() { UIBezierPath中的{1}}。 (不过,如果您实现此draw(_:)方法,请不要使用此方法的draw(_:)参数,而应始终参考该视图的rect。)

最底线,要么使用bounds(通过调用draw(_:)触发),要么使用setNeedsDisplay(仅更新其路径),但不要同时使用。


一些与我的代码段无关的观察结果:

  1. 您不需要检查CAShapeLayer中的!isHiddenisUserInteractionEnabled,因为如果按钮是隐藏的或禁用了用户交互,则不会调用此方法。正如the documentation所说:

    此方法将忽略隐藏的视图对象,禁用了用户交互或alpha级别小于0.01的视图对象。

    我还删除了hitTest中的alpha支票,因为这是非标准行为。没什么大不了的,但这是以后会困扰您的事情(例如,更改按钮基类,现在它的行为有所不同)。

  2. 您也可以将其设置为hitTest,以便可以在Interface Builder(IB)中看到它。如果仅以编程方式使用它,那没有什么害处,但为什么不也使其能够在IB中呈现呢?

  3. 我已将路径的配置移至@IBDesignable中。任何基于视图layoutSubviews的内容都应响应布局中的更改。当然,在您的示例中,您正在手动设置bounds,但这不是对此按钮类施加的不必要限制。您将来可能会使用自动版式,并且使用frame可确保其继续按预期运行。另外,通过这种方式,如果按钮的大小发生更改,路径也会被更新。

  4. 如果路径为直线,则没有必要检查layoutSubviews。因此,我添加了第三点,以便可以测试击中点是否在路径内。