2012-07-04 101 views
2

將當前手勢識別器附加到多個圖像視圖時,我今天遇到了一些奇怪的事情。它只附加到最後一個,換句話說,它只能附加到一個視圖!將手勢識別器附加到多個圖像視圖

我不得不創建多個手勢識別器來滿足我的要求。

以下是我所做的。我做得對嗎?這是將識別器附加到多個圖像視圖的唯一方法嗎?

請注意,我不想使用UITableView或UIVIew並將其中的所有圖像,並將手勢識別器僅附加到UITableView或UIVIew。我有所有圖像分散,我必須檢測哪個圖像被拖動。謝謝。

[imgView1 setUserInteractionEnabled:YES]; 
[imgView1 setMultipleTouchEnabled:YES]; 

[imgView2 setUserInteractionEnabled:YES]; 
[imgView2 setMultipleTouchEnabled:YES]; 

[imgView3 setUserInteractionEnabled:YES]; 
[imgView3 setMultipleTouchEnabled:YES]; 

[imgView4 setUserInteractionEnabled:YES]; 
[imgView4 setMultipleTouchEnabled:YES]; 

[imgView5 setUserInteractionEnabled:YES]; 
[imgView5 setMultipleTouchEnabled:YES]; 

[imgView6 setUserInteractionEnabled:YES]; 
[imgView6 setMultipleTouchEnabled:YES]; 


//Attach gesture recognizer to each imagviews 
gestureRecognizer1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; 
gestureRecognizer1.delegate = self; 

gestureRecognizer2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; 
gestureRecognizer2.delegate = self; 

gestureRecognizer3 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; 
gestureRecognizer3.delegate = self; 

gestureRecognizer4 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; 
gestureRecognizer4.delegate = self; 

gestureRecognizer5 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; 
gestureRecognizer5.delegate = self; 

gestureRecognizer6 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; 
gestureRecognizer6.delegate = self; 

[imgView1 addGestureRecognizer:gestureRecognizer1]; 
[imgView2 addGestureRecognizer:gestureRecognizer2]; 
[imgView3 addGestureRecognizer:gestureRecognizer3]; 
[imgView4 addGestureRecognizer:gestureRecognizer4]; 
[imgView5 addGestureRecognizer:gestureRecognizer5]; 
[imgView6 addGestureRecognizer:gestureRecognizer6]; 

回答

2

是的,每個手勢識別器有一個視圖。所以,如果你只想要一個識別器,把它放在上海華,例如:

UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; 
[self.view addGestureRecognizer:gestureRecognizer]; 

,然後在您的處理程序,您可以:

- (void)handleLongPress:(UILongPressGestureRecognizer *)sender 
{ 
    CGPoint location = [sender locationInView:self.view]; 

    if (sender.state == UIGestureRecognizerStateBegan) 
    { 
     for (UIView *view in self.view.subviews) 
     { 
      if ([view isKindOfClass:[UIImageView class]] && CGRectContainsPoint(view.frame, location)) 
      { 
       UIImageView *image = (UIImageView *) view; 

       // ok, now you know which image you received your long press for 
       // do whatever you wanted on it at this point 

       return; 
      } 
     } 
    } 
} 

順便說一句,如果你這樣做,你不需要擔心在圖像上啓用用戶交互。

最後,除非您要符合UIGestureRecognizerDelegate,否則不需要擔心指定您的手勢識別器的代表。另外請注意,我爲識別器使用了一個本地變量,因爲沒有理由掛在它上面。

更新:

雖然上面的代碼工作正常,或許更重要的是一個自定義的長按手勢識別,如果長按並沒有發生過的圖像(這樣它更是會失敗如果您在視圖中發現其他手勢識別器,可能會發揮出色)。所以:

#import <UIKit/UIGestureRecognizerSubclass.h> 

@interface ImageLongPressGestureRecognizer : UILongPressGestureRecognizer 
@property (nonatomic, weak) UIImageView *imageview; 
@end 

@implementation ImageLongPressGestureRecognizer 

@synthesize imageview = _imageview; 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    self.imageview = nil; 

    [super touchesBegan:touches withEvent:event]; 

    CGPoint location = [self locationInView:self.view]; 

    for (UIView *view in self.view.subviews) 
    { 
     if ([view isKindOfClass:[UIImageView class]] && CGRectContainsPoint(view.frame, location)) 
     { 
      self.imageview = (UIImageView *)view; 
      return; 
     } 
    } 

    self.state = UIGestureRecognizerStateFailed; 
} 

@end 

然後相應地創建您的手勢識別,使用這個新的子類:

ImageLongPressGestureRecognizer *gestureRecognizer = [[ImageLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; 
[self.view addGestureRecognizer:gestureRecognizer]; 

然後,作爲這個子類的一個不錯的效益,你的主要手勢識別被簡化,即:

- (void)handleLongPress:(ImageLongPressGestureRecognizer *)sender 
{ 
    if (sender.state == UIGestureRecognizerStateBegan) 
    { 
     // you can now do whatever you want with sender.imageview, e.g. this makes it blink for you: 

     [UIView animateWithDuration:0.5 
         animations:^{ 
          sender.imageview.alpha = 0.0; 
         } completion:^(BOOL finished){ 
          [UIView animateWithDuration:0.5 
               animations:^{ 
                sender.imageview.alpha = 1.0; 
               } 
               completion:nil]; 
         }]; 
    } 
} 
+0

非常感謝!我需要關心我不想添加手勢的圖像瀏覽!我可以使用觸摸位置來做到這一點。 – applefreak

+0

Omg,我今天才發現這個,我想知道爲什麼我的darn UITableViewCell只能用於最後一個被刷過的單元格:P – Zhang

2

您無法將手勢識別器附加到多個對象(如您發現的那樣)。你正在做的一個解決方案可能是UIImageView的子類,並在該類中設置代碼,以便每個視圖創建其識別器等。

+0

謝謝。創建子類是一個好主意。 – applefreak

0

我想,首先,你應該製作一個視圖數組和識別器數組(可變數組,如果需要的話),然後填充它。它將幫助您使用循環來避免代碼重複。

至於多視圖與一個識別器 - 不,這是不可能的,answered here

相關問題