2017-10-30 4 views
0

UIView에서 BezierPaths 내부의 탭을 감지하려고하고 containsPoint 메소드에 대한 많은 참조를 발견했습니다. 그러나 실제로 내 ViewController에서 BezierPaths를 참조하는 방법을 찾을 수없는 것 같습니다.UIView에서 BezierPath를 참조하는 방법

I가 설정 :

class func drawSA(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 69, height: 107), resizing: ResizingBehavior = .aspectFit, SACountries: [String: ViewController.CountryStruct]) 
{ 

    let myPath = UIBezierPath() 
    myPath.move(to: CGPoint(x: 32.24, y: 8.61)) 
    myPath.addLine(to: CGPoint(x: 31.99, y: 8.29)) 
    myPath.addLine(to: CGPoint(x: 31.78, y: 8.19)) 
    myPath.close() 
} 

베지이 함수에 그려, 호출 : 메인의 ViewController에서

override func draw(_ rect: CGRect) 

, I는 상기에 탭을 검출하는 기능에 따라 한 UIView :

@objc func SATap(sender: UITapGestureRecognizer) 
{ 
    let location = sender.location(in: self.SAView) 

    // how do I call containsPoint from here? 
} 

여기에서 containsPoint를 어떻게 호출합니까?

bezierPaths는 런타임에 올바르게 그려집니다.

+0

경로를보기의 속성으로 저장하십시오. 'self.SAView.myPath'로 containsPath를 호출합니다. – Larme

답변

0

경로가 별도의 클래스 내의 클래스 함수에서 만들어 지므로 뷰에 직접 저장할 수 없습니다.

UIBezierPaths를 사전에 넣은 다음 사전을 반환하여이 문제를 해결했습니다. 배열도 작동하지만이 방법을 통해 특정 경로에 쉽게 액세스 할 수 있습니다. 그때의 사전을 사용

for path in Paths.keys.sorted() 
    { 
     self.layer.addSublayer(CreateLayer(path)) // The Function simply creates a CAShapeLayer 
    } 

: 사전에 루프에서 들어 그들을 만들 때

class func drawSA(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 71, height: 120), resizing: ResizingBehavior = .aspectFit, SACountries: [String: ViewController.CountryStruct]) -> ([String: UIBezierPath]) 
{ 
    var Paths = [String: UIBezierPath]() 

    let myPath = UIBezierPath() 
    myPath.move(to: CGPoint(x: 32.24, y: 8.61)) 
    myPath.addLine(to: CGPoint(x: 31.99, y: 8.29)) 
    myPath.addLine(to: CGPoint(x: 31.78, y: 8.19)) 
    myPath.close() 

    Paths["mP"] = myPath 


    let myPath2 = UIBezierPath() 
    myPath2.move(to: CGPoint(x: 32.24, y: 8.61)) 
    myPath2.addLine(to: CGPoint(x: 31.99, y: 8.29)) 
    myPath2.addLine(to: CGPoint(x: 31.78, y: 8.19)) 
    myPath2.close() 

    Paths["mP2"] = myPath2 


    return Paths 
} 

나는 다음보기에서 레이어를 만들 수 View.addLayer을 사용하고 각 레이어에 Layer.name 속성을 추가 제스처 기능 :

@objc func ATap(sender: UITapGestureRecognizer) 
{ 
    let location = sender.location(in: self.SAView) 
    // You need to Scale Location if your CAShapeLayers are Scaled 

    for path in SAView.Paths 
    { 
     let value = path.value 

     value.contains(location) 

     if value.contains(location) 
     { 
      for (index, layer) in SAView.layer.sublayers!.enumerated() 
      { 
       if layer.name == path.key 
       { 
        // Do something when specific layer is Tapped 
       } 
      } 
     } 
    } 
} 

아니라이 일을 더 나은 방법이있을 수 있지만, 그건 모든 작업을 잘 수행한다.

관련 문제