2015-08-28 51 views
-3

我很困惑LongPressGestureRecognizer.I放置其中的一個滾動視圖,但它工作兩次。當我舉起我的手指,添加到它的方法再次調用。我想知道它只有第一次打電話。我該怎麼辦?任何幫助將不勝感激,謝謝。長按手勢識別器發射兩次

+3

顯示您的代碼以創建和應用手勢識別器和識別器處理程序的代碼。 – rmaddy

+0

是的,當你開始按下時有兩個叫做一個,當它結束時有兩個叫做一個 –

回答

2

先來看看蘋果公司的文檔不得不說一下吧: - 。

「長按手勢是連續手勢在指定的時間段(minimumPressDuration)中按下允許的手指的數量(numberOfTouchesRequired)並且觸摸不超過允許的移動範圍(allowableMovement)時,開始(UIGestureRecognizerStateBegan)當手勢識別器轉換到更改狀態時任何手指移動,當任何手指擡起時它結束(UIGestureRecognizerStateEnded)。「

- (void)LongPress:(UILongPressGestureRecognizer*)sender { 

     if (sender.state == UIGestureRecognizerStateBegan){ 
      NSLog(@"UIGestureRecognizerStateBegan."); 
     //in your case add your functionality over here 
     } 
     else if (sender.state == UIGestureRecognizerStateEnded) { 
      NSLog(@"UIGestureRecognizerStateEnded"); 
     //if you want to add some more functionality when gesture got ended. 

     } 

     } 
1

UILongPressGestureRecognizer不像UITapGestureRecognizer一樣。它包含一些狀態。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2); 
    [self.view addSubview:scrollView]; 

    UILongPressGestureRecognizer *lpGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lpHandler:)]; 
    [scrollView addGestureRecognizer:lpGes]; 
} 

- (void)lpHandler:(UILongPressGestureRecognizer *)lpGes 
{ 
    switch (lpGes.state) { 
     case UIGestureRecognizerStateBegan: 
      NSLog(@"UILongPressGestureRecognizer: began"); 
      break; 

     case UIGestureRecognizerStateEnded: 
      NSLog(@"UILongPressGestureRecognizer: ended"); 
      break; 

     default: 
      break; 
    } 
} 

對於上面的代碼,你將得到2日誌:

2015-08-28 12:22:39.084 aaaaa[50704:2339282] UILongPressGestureRecognizer: began 
2015-08-28 12:22:40.687 aaaaa[50704:2339282] UILongPressGestureRecognizer: ended