2013-02-01 21 views
7

我正在爲iPhone編寫Objective-C程序。UILongPressGestureRecognizer將不會響應觸摸和鎖定

我想實現一個UILongPressGestureRecognizer,並不能讓它按照我想要的方式工作。

做的是簡單的:

響應觸摸是舉行下在屏幕上。

UILongPressGestureRecognizer工作得很好,只要觸摸移動,觸摸開始,但如果我在同一個地方按住觸摸,什麼都不會發生。

爲什麼?

我該如何處理開始,不移動,並保持在同一個地方?

這是我目前的代碼。


// Configure the press and hold gesture recognizer 
touchAndHoldRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(touchAndHold:)]; 
touchAndHoldRecognizer.minimumPressDuration = 0.1; 
touchAndHoldRecognizer.allowableMovement = 600; 
[self.view addGestureRecognizer:touchAndHoldRecognizer]; 
+0

//配置按住手勢識別器 touchAndHoldRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(touchAndHold :)]; touchAndHoldRecognizer.minimumPressDuration = 0.1; touchAndHoldRecognizer.allowableMovement = 600; [self.view addGestureRecognizer:touchAndHoldRecognizer]; –

+0

我的觀點正是如此。如果你不移動,你不會重複發生。這就是我想要解決的問題。如果我不移動,你知道一種方法可以讓我重複發生嗎? –

+2

這應該重新打開,因爲如果你仔細看看這個問題的要求,它不是問「我該如何做一個長按手勢」,而是「現在我在長按手勢中獲得通知,甚至儘管我沒有動。「這不是其他問題的重複。 – Rob

回答

12

您所描述的行爲,你的手勢識別沒有收到,當你不動你的處理器還呼籲是標準的行爲。這些手勢的state屬性在移動時屬於UIGestureRecognizerStateChanged類型,所以如果事情沒有改變,您的處理程序將不會被調用。

你可以

  • 在調用與UIGestureRecognizerStateBegan開始重複的計時器state你的手勢識別;
  • 打電話給您的手勢識別器stateUIGestureRecognizerStateCancelled,UIGestureRecognizerStateFailedUIGestureRecognizerStateEnded然後invalidate並釋放計時器;
  • 確保手勢識別方法是節約的任何值,你正在尋找一些類屬性(的locationInView例如值或其他)

所以,也許是這樣的:

@interface ViewController() 

@property (nonatomic) CGPoint location; 
@property (nonatomic, strong) NSTimer *timer; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
    gesture.minimumPressDuration = 0.1; 
    gesture.allowableMovement = 600; 
    [self.view addGestureRecognizer:gesture]; 
} 

- (void)handleTimer:(NSTimer *)timer 
{ 
    [self someMethod:self.location]; 
} 

- (void)handleGesture:(UIGestureRecognizer *)gesture 
{ 
    self.location = [gesture locationInView:self.view]; 

    if (gesture.state == UIGestureRecognizerStateBegan) 
    { 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES]; 
    } 
    else if (gesture.state == UIGestureRecognizerStateCancelled || 
      gesture.state == UIGestureRecognizerStateFailed || 
      gesture.state == UIGestureRecognizerStateEnded) 
    { 
     [self.timer invalidate]; 
     self.timer = nil; 
    } 

    [self someMethod:self.location]; 
} 

- (void)someMethod:(CGPoint)location 
{ 
    // move whatever you wanted to do in the gesture handler here. 

    NSLog(@"%s", __FUNCTION__); 
} 

@end 
+0

...我想我已經開始明白了。我的問題是,我不瞭解蘋果的方法與他們如何處理觸摸。我認爲我今天花4小時在互聯網上尋找答案並越來越沮喪的原因是我錯過了這一點。你幫我看看這個Rob。 Cocoa沒有像「handleTouchesRemainedStationary」這樣的方法,因爲如果觸摸還沒有結束,並且沒有移動...那麼__明顯___它保持不變。你可以繼續迭代你的執行循環。謝謝,羅伯! –

+0

@ J-Rock酷。您的實現可能會有所不同(例如,您可能會在手勢時重置計時器,因此如果您正在移動,則不會收到計時器呼叫),但您明白了。僅供參考,我簡化了我的代碼。 – Rob

+0

@ J-Rock最後一個觀察,我之前提到過。如果你想要一個連續的手勢,在你觸摸屏幕後很快開始並在你四處移動時繼續,你可能需要使用'UIPanGestureRecognizer'。如果這對你有用,那就更有意義了。大多數程序員會被使用長按手勢識別器誤導600個點。如果由於某種原因必須使用長按(例如,在開始時等待0.1這很重要),則使用它,否則,請考慮使用'UIPanGestureRecognizer'。 – Rob