0

我尝试使以下工作:

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"); }

有人可以帮我吗?

4

2 回答 2

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");
}

工作完美。,希望这会有所帮助..

于 2013-03-14T04:01:14.310 回答
0

我自己发现的:

[vw bringSubviewToFront:img];

是解决方案。现在它正在工作。

于 2013-03-15T14:25:12.957 回答