我尝试使以下工作:
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"); }
有人可以帮我吗?