启用了UIImageView用户交互,但仍然可以点击手势识别

时间:2013-03-13 23:50:42

标签: uiimageview uitapgesturerecognizer

我尝试了以下工作:

vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [ass.match.ch.shots intValue]*(50+spacer), 20)];

for(int i =index;i<index+[ass.match.ch.shots intValue];i++){
    Results *theRes = [[ass.mem.results allObjects] objectAtIndex:i];
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(a*(50+spacer), 0, 50, 50)];
    if([theRes.singleresult intValue] > 0){
        [img setImage:[UIImage imageNamed:@"hit.png"]];
        [img.layer setShadowColor:[[UIColor greenColor] CGColor]];
    }else{
        [img setImage:[UIImage imageNamed:@"nhit.png"]];
        [img.layer setShadowColor:[[UIColor redColor] CGColor]];
    }

    UITapGestureRecognizer *tap =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];


    img.userInteractionEnabled = YES;
    [img setContentMode:UIViewContentModeCenter];
    [img.layer setShadowOffset:CGSizeMake(-1.0, -1.0)];
    [img.layer setShadowOpacity:0.5];
    [img addGestureRecognizer:tap];
    [vw addSubview:img];
    a++;
}

[vw setCenter:CGPointMake((self.view.frame.size.width-200)/2,150)];
[vw setUserInteractionEnabled:YES];

[self.view addSubview:vw];

但是当我点击任何创建的ImageViews并且我不知道原因时,这个函数没有被调用:

-(IBAction)imageTapped:(id)sender{
NSLog(@"Test"); }

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

你必须改变你的功能,这是你的解决方案..

UIButton *btn = [[UIButton alloc]init];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50,50,70,70);
btn.backgroundColor = [UIColor blackColor];
[btn addTarget:self action:@selector(nextview:)forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

-(void) nextview:(id)sender {
NSLog(@"tapped");
}

完美地工作。希望这有帮助..

答案 1 :(得分:0)

我自己发现了:

[vw bringSubviewToFront:img];

是解决方案。现在它正在发挥作用。