2012-05-06 46 views
0


我在我的iOS應用程序上有一個單獨的視圖,裏面有一個mapView。 添加輕擊或長按識別器時,會正確調用事件。 但不可用掐事件......UIPinchGestureRecognizer不叫

UIPinchGestureRecognizer *handlePinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:mapView action:@selector(handleGesture:)]; 
    [mapView addGestureRecognizer:handlePinchGesture]; 

任何想法,我要補充的嗎? 謝謝。

+0

顯示'handlGesture:'方法 – CodaFi

+0

添加NSLog()進行調試。 – c0d3Junk13

+0

handleGesture函數沒有被調用(我已經在第1行設置了一個斷點,並且沒有到達)。 –

回答

2

假設你mapViewMKMapView,它具有縮放地圖自身的縮放手勢識別。

如果你想添加你自己的識別器,你必須允許它與另一個(mapview控制的)識別器同時識別。設置您的手勢識別器的delegate並實施gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:(您可以隨時返回YES)。

您還應該設置self作爲手勢識別器的target而不是mapView

+0

即使添加此函數也不會觸發我的handlePinchGesture函數(我在第1行設置了一個斷點,但未到達)。所以我添加了一個handlePinchGesture委託給自己,但現在當我捏,代碼崩潰...在我的xib中,我沒有委託。它可以鏈接? –

+0

究竟是什麼導致代碼崩潰?你有沒有設置委託?只是在不設置代理的情況下實現此方法將不會執行任何操作... – omz

+0

「發送給實例的無法識別的選擇器」。在我的xib中,我找不到委託對象... –

1

在handleGesture方法做,你做這樣的事情:

CGFloat beginPinch; //declare this as your ivars 

-(void)handleGesture:(UIPinchGestureRecognizer *)pinchRecognizer 
{ 
    if (pinchRecognizer.state == UIGestureRecognizerStateBegan) 
    { 
     beginPinch = pinchRecognizer.scale;  
    } 
    else if (pinchRecognizer.state == UIGestureRecognizerStateEnded) 
    { 
     if (pinchRecognizer.scale < beginPinch) 
     { 
       //do your stuff 
     } 
    } 
} 
+0

不幸的是,即使是第一線也沒有達到。該函數未被調用。 –

+0

看着omz發現了這個錯誤。應該親自抓到它! – user523234