2012-09-06 39 views
0

在我的應用程序中,我使用一個按鈕,我分配了兩種方法,他們之一,當你觸摸(按鈕圖像被改變)工作,另一個工作時,當你觸摸在裏面(另一個視圖被打開)。簡單地說,如果你想打開一個視圖,你可以按下按鈕,但是當你觸摸按鈕時,圖像會被改變,並且在你擡起手指之後,另一個視圖被打開。我的問題是,如果按下按鈕圖像被更改,但是如果您將手指移動到遠離按鈕的位置,則內部觸摸不起來,因爲它應該是。但問題在於圖像堅持其超版本,因爲觸發一次就會觸發。我該怎麼辦?謝謝iphone開發:使用觸摸並同時觸摸內

回答

3

您可以在控制狀態touchDragOutsidetouchDragExit中處理此操作,具體取決於您希望執行的操作。使用touchDragOutside,您可以檢測用戶何時觸摸按鈕內部並拖動手指而不離開按鈕的可觸摸邊界,並且touchDragExit可以檢測到它們何時拖出按鈕可觸摸邊界。

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragExit]; 
[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragOutside]; 
0

我面臨這個問題我自己,主要是我們使用這些事件: -

//此事件正常工作和火災

[按鈕addTarget:自我行動:@selector(壓緊)forControlEvents:UIControlEventTouchDown ]。

//這不火都

[按鈕addTarget:自我行動:@selector(holdRelease)forControlEvents:UIControlEventTouchUpInside]。

解決方案: -

使用長按手勢識別器: -

UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleBtnLongPressgesture:)]; 
[button addGestureRecognizer:btn_LongPress_gesture]; 

執行的手勢: -

- (void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{ 


//as you hold the button this would fire 

if (recognizer.state == UIGestureRecognizerStateBegan) { 

    [self someMethod]; 
} 

//as you release the button this would fire 

if (recognizer.state == UIGestureRecognizerStateEnded) { 

    [self someMethod]; 
} 
}