2017-12-18 11 views
1

커스텀 어노테이션 view.How에서 버튼 클릭 이벤트를 찾으려고 노력할 것입니다 This.In this에서 Button click 이벤트를 찾을 수 없습니다. 제발 좀 힌트주세요.커스텀 어노테이션 뷰를 수행하는 방법 버튼 찾기 이벤트를 찾을 수 없습니까?

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 

     let overlays = self.mapVW.overlays 
     self.mapVW.removeOverlays(overlays) 

     if view.annotation is MKUserLocation 
     { 
      // Don't proceed with custom callout 
      return 
     } 

     let customeView = view.annotation as! Artwork 
     let viewCustome = Bundle.main.loadNibNamed("mapPopUp", owner: nil, options: nil) 

     let callOutView = viewCustome![0] as! CustomCalloutView 
     callOutView.lblLocationName.text = customeView.title 
     callOutView.lblCategory.text = customeView.category 
     callOutView.lblDistance.text = customeView.distance 

     let button = UIButton(frame: callOutView.lblLocationName.frame) 
     button.addTarget(self, action: #selector(GayGuideViewController.btnRouteView(sender:)), for: .touchUpInside) 
     callOutView.addSubview(button) 

     callOutView.center = CGPoint(x: view.bounds.size.width/5, y: -callOutView.bounds.size.height*0.52) 
     view.addSubview(callOutView) 
     mapVW.setCenter((view.annotation?.coordinate)!, animated: true) 
    } 

미리 감사드립니다.

+0

'customeView'에'gesture recognizer'를 추가 했습니까? –

+0

제스처 인식기를 사용하고 있지 않습니다. –

+0

시도했지만 시도하지 못했습니다 @ biloshkurskyi.ss –

답변

1

사용자 정의 팝업보기에서 click 이벤트에 hitTest 메소드를 사용하고있었습니다. 나는 그 시간에 통지 관찰자를 추가하고 나의 행동 이벤트를 수행한다는 점을 발견했다.

class CAnnotationView: MKAnnotationView 
     { 
      override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 
       let hitView = super.hitTest(point, with: event) 
       if (hitView != nil) 
       { 
        self.superview?.bringSubview(toFront: self) 
       } 
       return hitView 
      } 
      override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { 
       let rect = self.bounds 
       var isInside: Bool = rect.contains(point) 
       if(!isInside) 
       { 
        for view in self.subviews 
        { 
         isInside = view.frame.contains(point) 
         if isInside 
         { 
          let dictionary = NSMutableDictionary() 
          dictionary.setValue(self.annotation?.coordinate.latitude, forKey: "lat") 
          dictionary.setValue(self.annotation?.coordinate.longitude, forKey: "long") 

          NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Noti_Coordinate"), object: nil, userInfo: dictionary as? [AnyHashable : Any]) 
          break 
         } 
        } 
       } 
       return isInside 
      } 
     } 


override func viewDidLoad() 
    { 
       NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "Noti_Coordinate"), object: nil) 
      NotificationCenter.default.addObserver(self, selector: #selector(notificationForRoute(noti:)), name: NSNotification.Name(rawValue: "Noti_Coordinate"), object: nil) 
    } 
func notificationForRoute(noti : NSNotification) 
    { 
     let dict = noti.userInfo! as NSDictionary 
     let lat = dict.value(forKey: "lat") 
     let long = dict.value(forKey: "long") 

     let coordinate = CLLocationCoordinate2D(latitude: lat as! CLLocationDegrees, longitude: long as! CLLocationDegrees) 

     let overlays = self.mapVW.overlays 
     self.mapVW.removeOverlays(overlays) 

     route(dest: coordinate) 
    }