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