我的UILongPressGestureRecognizer
中的allowableMovement
屬性似乎被忽略。我使用Single View Application模板創建了一個新項目(Xcode 4.5.1,iOS 6),並在視圖中添加了長按手勢識別器。有一個連接線和一個動作。下面是操作方法的代碼:allowableMovement似乎被忽略
- (IBAction)longPress:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStatePossible) NSLog(@"possible");
if (sender.state == UIGestureRecognizerStateBegan) NSLog(@"began");
if (sender.state == UIGestureRecognizerStateChanged) NSLog(@"changed");
if (sender.state == UIGestureRecognizerStateRecognized) NSLog(@"recognized");
if (sender.state == UIGestureRecognizerStateCancelled) NSLog(@"cancelled");
if (sender.state == UIGestureRecognizerStateFailed) NSLog(@"failed");
CGPoint locationInView = [sender locationInView:self.view];
NSLog(@"long press: allowableMovement= %f, x= %f, y= %f", sender.allowableMovement, locationInView.x, locationInView.y);
}
如果我按下足夠長的時間放手我在日誌中得到這樣的:
2012-10-30 20:24:41.449 Long Press[1078:907] began
2012-10-30 20:24:41.455 Long Press[1078:907] long press: allowableMovement= 10.000000, x= 210.500000, y= 99.500000
2012-10-30 20:24:42.880 Long Press[1078:907] recognized
2012-10-30 20:24:42.882 Long Press[1078:907] long press: allowableMovement= 10.000000, x= 208.500000, y= 96.000000
這是我所期望的那樣。
但無論我將allowableMovement
設置爲(正面,負面,大,小),一旦狀態爲UIGestureRecognizerStateBegan
,我就可以將手指拖到屏幕上。狀態更改爲UIGestureRecognizerStateChanged
,並且頻繁更新,並且locationInView繼續準確跟蹤。當我放手的時候,我得到了UIGestureRecognizerStateRecognized
狀態,並且最終輸出到了日誌中。
該類參考指出,如果移動超過allowableMovement
,識別器將失敗。爲什麼allowableMovement
屬性似乎被忽略?
@ Murray Sagal:這也解決了我的疑惑。想知道,如果有一種方法可以檢測用戶是否在LongPressGesture開始後將他們的手指拖出視圖。 – Kashif
@TPOS我認爲你可以使用'locationInView:'或其中一種方法。但它聽起來像是值得自己的問題。 –