3 回答

TA貢獻1155條經驗 獲得超0個贊
UILongPressGestureRecognizer是連續事件識別器。您必須查看狀態以查看這是事件的開始,中間還是結束,并采取相應的措施。即,您可以在開始之后放棄所有事件,或者僅根據需要查看運動。從 類參考:
長按手勢是連續的。當在指定時間段內(minimumPressDuration)按下了允許的手指數(numberOfTouchesRequired),并且觸摸沒有移動超出允許的移動范圍(allowableMovement)時,手勢即開始(UIGestureRecognizerStateBegan)。每當手指移動時,手勢識別器都會轉換為“更改”狀態,并且在任何手指抬起時手勢識別器都會終止(UIGestureRecognizerStateEnded)。
現在您可以像這樣跟蹤狀態
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
//Do Whatever You want on End of Gesture
}
else if (sender.state == UIGestureRecognizerStateBegan){
NSLog(@"UIGestureRecognizerStateBegan.");
//Do Whatever You want on Began of Gesture
}
}

TA貢獻1865條經驗 獲得超7個贊
要檢查UILongPressGestureRecognizer的狀態,只需在選擇器方法上添加if語句:
- (void)handleLongPress:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Long press Ended");
} else if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"Long press detected.");
}
}

TA貢獻1934條經驗 獲得超2個贊
您需要檢查正確的狀態,因為每種狀態都有不同的行為。您最有可能需要UIGestureRecognizerStateBegan帶有狀態UILongPressGestureRecognizer。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[myView addGestureRecognizer:longPress];
[longPress release];
...
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
if(UIGestureRecognizerStateBegan == gesture.state) {
// Called on start of gesture, do work here
}
if(UIGestureRecognizerStateChanged == gesture.state) {
// Do repeated work here (repeats continuously) while finger is down
}
if(UIGestureRecognizerStateEnded == gesture.state) {
// Do end work here when finger is lifted
}
}
- 3 回答
- 0 關注
- 560 瀏覽
添加回答
舉報