我希望我的swift代码显示一个uibezierPath按钮。该代码使用override func draw来绘制按钮。代码出现编译错误。它告诉我我在let customButton=FunkyButton(编码器:<#NSCoder#>(中缺少一个参数,你可以在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()
——draw(_:)
中的UIBezierPath
。(不过,如果实现此draw(_:)
方法,请不要使用此方法的rect
参数,而是始终返回视图的bounds
。(
最重要的是,使用draw(_:)
(通过调用setNeedsDisplay
触发(或使用CAShapeLayer
(只更新其路径(,但不要同时执行这两种操作。
与我的代码片段相关的一些不相关的观察结果:
您不需要在
hitTest
中检查!isHidden
或isUserInteractionEnabled
,因为如果按钮被隐藏或禁用了用户交互,则不会调用此方法。正如文件所说:此方法忽略隐藏的、已禁用用户交互或alpha级别小于0.01的视图对象。
我还删除了
hitTest
中的alpha
检查,因为这是非标准行为。这不是什么大不了的事,但这是以后会让你头疼的事情(例如,更改按钮基类,现在它的行为不同了(。您不妨将其设为
@IBDesignable
,以便在Interface Builder(IB(中看到它。如果你只是以编程方式使用它,那也没有什么害处,但为什么不让它也能在IB中呈现呢?我已将路径的配置移动到
layoutSubviews
中。任何基于视图的bounds
的内容都应该响应布局中的更改。当然,在您的示例中,您手动设置frame
,但这是对该按钮类的不必要限制。您将来可能会使用自动布局,使用layoutSubviews
可以确保它继续按预期工作。此外,通过这种方式,如果按钮的大小发生变化,路径将被更新。如果路径是一条线,那么检查
contains
是没有意义的。所以,我添加了第三个点,这样我就可以测试命中点是否在路径内。