2017-05-02 8 views
0

MKMapViewを使用してマップアプリケーションを構築していますが、MKAnnotationViewのサブビューで相互作用ができないようです。MKAnnotationViewのサブビューとしてのUIButtonが機能しない

私はMKAnnotationViewの主なサブビューとしてUIViewクラスを持って、そこに私はこのように、別のサブビュー、UIButtonを持っている:

let button = UIButton(type: .infoLight) 
button.frame = CGRect(x: 0, y: 0, width: 30, height: 30) 
button.addTarget(self, action: #selector(self.buttonTapped(sender:)), for: UIControlEvents.touchUpInside) 
//self.bringSubview(toFront: button) 
//button.isUserInteractionEnabled = true 
self.addSubview(button) 

(コメント行を助けていないようだ)

は、次にクラスのタップ機能は、次のようになります。buttonTapped()メソッドが起動しませんなぜ

func buttonTapped(sender: UIButton!) { 
    print("button tapped:", sender) 
} 

任意のアイデア?

答えて

0

mapkitがマップにボタンを追加すると、マップがラスタライズされ、ビューとして追加されると思います。そのため、「ボタン」はイベントに触れることができなくなりました。

これはtapGestureRecognizerを使用して実装できます。

mapkitが次の関数でビューを要求するとき。 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

タップジェスチャーが追加されたUiviewを使用します。

+0

ありがとう、私はそれを試してみよう! – Hexagons

関連する問題