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)
}
}
}
}
왜 당신이 이런 식으로해야합니까 alwaysTemplate하는? 이미지를 사용하는 것이 더 간단하지 않습니까? –
터치가 화살표 프레임 안에 있는지 정말로 확인하고 싶습니까? 나는 단지 이미지를 사용할 것이다. –