3 回答

TA貢獻(xiàn)1809條經(jīng)驗 獲得超8個贊
UILongPressGestureRecognizer是連續(xù)事件識別器。您必須查看狀態(tài)以查看這是事件的開始,中間還是結(jié)束,并采取相應(yīng)的措施。即,您可以在開始之后放棄所有事件,或者僅根據(jù)需要查看運動。從 類參考:
長按手勢是連續(xù)的。當(dāng)在指定時間段內(nèi)(minimumPressDuration)按下了允許的手指數(shù)(numberOfTouchesRequired),并且觸摸沒有移動超出允許的移動范圍(allowableMovement)時,手勢即開始(UIGestureRecognizerStateBegan)。每當(dāng)手指移動時,手勢識別器都會轉(zhuǎn)換為“更改”狀態(tài),并且在任何手指抬起時手勢識別器都會終止(UIGestureRecognizerStateEnded)。
現(xiàn)在您可以像這樣跟蹤狀態(tài)
- (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貢獻(xiàn)1883條經(jīng)驗 獲得超3個贊
要檢查UILongPressGestureRecognizer的狀態(tài),只需在選擇器方法上添加if語句:
- (void)handleLongPress:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Long press Ended");
} else if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"Long press detected.");
}
}

TA貢獻(xiàn)1821條經(jīng)驗 獲得超6個贊
您需要檢查正確的狀態(tài),因為每種狀態(tài)都有不同的行為。您最有可能需要UIGestureRecognizerStateBegan帶有狀態(tài)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 關(guān)注
- 946 瀏覽
添加回答
舉報