UITapGestureRecognizer不会在以编程方式创建的UIImageView上触发

时间:2013-04-23 09:45:03

标签: iphone ios uiimageview xcode4.5 uitapgesturerecognizer

我正在使用XCode 4.5.1。

我在.h文件和.m文件的开头添加了<UIGestureRecognizerDelegate>

@interface FirstViewController ()

@property (nonatomic, strong) UIImageView *img1;

@end

然后在viewDidLoad中,以编程方式创建UIImageView

self.img1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, -30, 44, 44)];
[self.img1 setImage:[UIImage imageNamed:@"image1.png"]];
[self.img1 setAlpha:0.8];
[self.img1 setHidden:NO];
[self.img1 setUserInteractionEnabled:YES];
[self.img1 setTag:901];

然后

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(guessTapObject:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
tap.delegate = self;

[self.img1 addGestureRecognizer:tap];

[self.view addSubview:self.img1];
[self.view bringSubviewToFront:self.img1];

并在最后

- (void)guessTapObject:(UITapGestureRecognizer *) gesture
{
    // guessing the image
    UIImageView *tapImageView = (UIImageView*) gesture.view;
    NSLog(@"Gesture Tag: %d", tapImageView.tag);
}

我有4个不同的图像,并按上述方式以编程方式创建它们。并应用动画将它们从上到下移动。

我希望当用户点击图像时,它应该动画并消失(我知道代码)。但是代码没有触发tap事件。我把断点放在guessTapObject函数的开头,但是不要去那里。

UITapGestureRecognizer声明时,调试显示guessTapObject已附加到识别器以执行点击操作。

请帮助为什么tap事件没有触发其选择器方法???

提前感谢您的帮助。


编辑: 我也尝试使用for循环为每个图像视图分别设置手势识别器

for (UIView * view in self.view.subviews) {
    if (view.tag > 900){
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(guessTapObject:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
    //    tap.delegate = self;
        [view addGestureRecognizer:tap];
        NSLog(@"tapView: %@", tap.view);
        NSLog(@"tap: %i", tap.enabled);
    }
}

3 个答案:

答案 0 :(得分:5)

我遇到了类似的问题。确保将userView设置为true,用于userInteraction ..

imageView.userInteractionEnabled = YES;

它对我有用!

答案 1 :(得分:2)

检查
将以下代码添加到View Controller

  • 在viewController.h文件中

    @property (nonatomic, strong) UIImageView *img1;
    @property (nonatomic, strong) UIImageView *img2;
    @property (nonatomic, strong) UIImageView *img3;
    
  • 在viewController.m文件中
    内部viewDidLoad方法

    // Init Image 1 
    self.img1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 30, 44, 44)];
    [self.img1 setImage:[UIImage imageNamed:@"image1_name.jpg"]];
    [self.img1 setAlpha:0.8];
    [self.img1 setHidden:NO];
    [self.img1 setUserInteractionEnabled:YES];
    [self.img1 setTag:901];
    
    // Init Image 2
    self.img2 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 30, 44, 44)];
    [self.img2 setImage:[UIImage imageNamed:@"image2_name.jpg"]];
    [self.img2 setAlpha:0.8];
    [self.img2 setHidden:NO];
    [self.img2 setUserInteractionEnabled:YES];
    [self.img2 setTag:902];
    
    // Init Image 3
    self.img3 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 130, 44, 44)];
    [self.img3 setImage:[UIImage imageNamed:@"image3_name.jpg"]];
    [self.img3 setAlpha:0.8];
    [self.img3 setHidden:NO];
    [self.img3 setUserInteractionEnabled:YES];
    [self.img3 setTag:903];
    
    // Add Images to view
    [self.view addSubview:self.img1];
    [self.view bringSubviewToFront:self.img1];
    
    [self.view addSubview:self.img2];
    [self.view bringSubviewToFront:self.img2];
    
    [self.view addSubview:self.img3];
    [self.view bringSubviewToFront:self.img3];
    
    // Set Tap Gesture to each image of view
    for (UIView * view in self.view.subviews) {
      if (view.tag > 900){
         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
                                          initWithTarget:self 
                                          action:@selector(guessTapObject:)];
         tap.numberOfTapsRequired = 1;
         tap.numberOfTouchesRequired = 1;
         [view addGestureRecognizer:tap];
         NSLog(@"tapView: %@", tap.view);
         NSLog(@"tap: %i", tap.enabled);
      }
    }
    


确保在for循环之前添加图像(使用子视图阵列添加TapGesture)

答案 2 :(得分:0)

我通过在视图上应用手势来解决,而不是分别在每个图像视图中应用。

[self.view addGestureRecognizer:tap];

然后在处理程序/选择器guessTapObject函数中,我使用了这个

CGPoint tapLocation = [gesture locationInView:self.view]; // gives the tap coordinates
for (UIView * view in self.view.subviews) { // gives view by iterating on each subview
    CGRect dropRect = [[[view layer] presentationLayer] frame]; // specific way of getting the frame with respect to its layer

    if(CGRectContainsPoint(dropRect, tapLocation)){ // checks for tap in the boundaries of view frame
        if (view.tag > 900) // my specific view
             // done what i want to do with that specific one
             // as i removed it from my superview
        }
    }
}