2017-10-28 4 views
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) 
      } 
     } 
    } 
} 
+0

왜 당신이 이런 식으로해야합니까 alwaysTemplate하는? 이미지를 사용하는 것이 더 간단하지 않습니까? –

+0

터치가 화살표 프레임 안에 있는지 정말로 확인하고 싶습니까? 나는 단지 이미지를 사용할 것이다. –

답변

0

그냥 프로젝트의 자산에서 제공하는 이미지를 넣어 :

enter image description here 이 사용자 정의 버튼 클래스 내 코드입니다. 그리고 단추 이미지를 자산의 이미지로 설정하십시오.

self.imageView.image = UIImage(named: "imageName") 

그리고 당신은 설정할 수있는 이미지의 라인의 색상을 변경하려는 경우 이미지 렌더링

self.imageView?.image = UIImage(named: "imageName")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate) 
self.imageView?.tintColor = .red 
관련 문제