3

我正在使用 XCode 4.5.1 。

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

@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 个不同的图像,并如上所述以编程方式创建它们。并应用动画将它们从上到下移动。

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

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

请帮忙为什么点击事件没有触发它的选择器方法???

提前感谢您的帮助。


编辑:我还尝试使用 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);
    }
}
4

3 回答 3

5

我遇到了类似的问题。确保 userInteraction 的 imageView 设置为 true 。

imageView.userInteractionEnabled = YES;

它对我有用!

于 2013-10-28T05:46:58.917 回答
2

检查此
将以下代码添加到您的视图控制器

  • 在 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)

于 2013-04-23T11:10:48.790 回答
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
        }
    }
}
于 2013-04-23T11:53:20.043 回答