0
xibにUIButtonがあり、四角形のように矢印があるようにフレームを変更したいが、それをxib(インターフェイスビルダーから)に保存したいのですがどうすればいいですか?drawRectを使わずにUIButton rectを変更する
私はdrawRectのUIBezierPathでそれを行うことができますが、それはカスタムシェイプを作成してコードから追加することだけです。
別の方法がありますか?
class ButtonContinue: UIButton {
var path: UIBezierPath!
override func awakeFromNib() {
backgroundColor = UIColor.green
addTarget(self, action: #selector(touchDown), for: .touchDown)
}
override func draw(_ rect: CGRect) {
path = UIBezierPath()
path.move(to: CGPoint(x: 32, y: 28 + (rect.size.height/2)))
path.addLine(to: CGPoint(x: 70, y: 28))
path.addLine(to: CGPoint(x: rect.size.width, y: 28))
path.addLine(to: CGPoint(x: rect.size.width, y: rect.size.height + 28))
path.addLine(to: CGPoint(x: 70, y: rect.size.height + 28))
path.close()
let shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.path = path.cgPath
layer.addSublayer(shapeLayer)
}
func touchDown(button: ButtonContinue, event: UIEvent) {
if let touch = event.touches(for: button)?.first {
let location = touch.location(in: button)
if path.contains(location) == false {
button.cancelTracking(with: nil)
}
}
}
}
をレンダリングした画像を設定することができ、画像の線の色を変更したい場合は?イメージを使うのは簡単ではありませんか? –
タッチが矢印の枠内にあるかどうか確認したいのですか?私はただのイメージを使用します。 –