【问题标题】:missing argument for parameter in coding error in trying to subclass a uibezierPath在尝试子类化 uibezierPath 时,编码错误中缺少参数的参数
【发布时间】:2026-01-15 22:35:01
【问题描述】:

我希望我的 swift 代码显示一个 uibezierPath 按钮。该代码使用 override func draw 来绘制按钮。代码出现编译错误。它告诉我我在 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 实现一个override func init(..),然后用let customButton = FunkyButton() 分配它。
  • 我对如何实现该代码感到困惑,请您写一个答案。
  • @Rob 你能提供你的指示作为答案吗?我会给你信用。

标签: swift subclass draw uibezierpath cgrect


【解决方案1】:

在实例化 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 in draw(_:) . (不过,如果你实现了这个draw(_:) 方法,不要使用这个方法的rect 参数,而是总是参考视图的bounds。)

底线,要么使用draw(_:)(通过调用setNeedsDisplay 触发)或使用CAShapeLayer(并且只更新其路径),但不要同时使用。


与我的代码 sn-p 相关的一些不相关的观察:

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

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

    我还删除了hitTest 中的alpha 签入,因为这是非标准行为。这没什么大不了的,但这是稍后会咬你的东西(例如,更改按钮基类,现在它的行为有所不同)。

  2. 您不妨将其设为@IBDesignable,以便您可以在 Interface Builder (IB) 中看到它。如果您只是以编程方式使用它并没有什么坏处,但为什么不让它也能够在 IB 中呈现呢?

  3. 我已将路径配置移至layoutSubviews。任何基于视图bounds 的东西都应该响应布局的变化。当然,在您的示例中,您正在手动设置frame,但这是对该按钮类的不必要限制。您将来可能会使用自动布局,并且使用 layoutSubviews 可确保它将继续按预期运行。另外,这样,如果按钮的大小发生变化,路径将被更新。

  4. 如果路径是一条线,则检查 contains 是没有意义的。所以,我添加了第三个点,以便我可以测试命中点是否在路径内。

【讨论】: