2013-04-23 39 views
3

我正在使用XCode 4.5.1。以编程方式创建的UIImageView上未触发的UITapGestureRecognizer

我的.h文件和.m文件开始在viewDidLoad添加<UIGestureRecognizerDelegate>

@interface FirstViewController() 

@property (nonatomic, strong) UIImageView *img1; 

@end 

然后,创建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循环中分别具有手势识别每一个ImageView的

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); 
    } 
} 
+0

我试过你发布的代码,它对我来说工作正常。我正在回调。如果您使用多个图像,则需要创建不同的UITapGestureRecognizer。对于一张图片,代码应该可以正常工作,请检查是否有任何东西是零,或者你的代码没有被调用。 – Amit 2013-04-23 10:02:23

+0

这是行之有效的,确保在你将它设置为-30时检查你正在点击的图像,检查一下。 – 2013-04-23 10:05:05

+0

@ amit3117 ....我尝试使用for循环,为每个图像添加单独的gesturerecognizer,但在我的结尾不起作用。 – eagle 2013-04-23 10:15:01

回答

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 
     } 
    } 
} 
2

入住这
添加以下代码到您的视图控制器

  • In viewCo ntroller.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); 
        } 
    } 
    


确保图像应为前循环添加(使用子视图阵列添加TapGesture)

5

我被卡在类似的问题..确保ImageView的设置为用户交互真..

imageView.userInteractionEnabled = YES; 

它为我工作!

相关问题