2013-03-13 95 views
0

我试图让下面的工作:的UIImageView用户交互,但仍挖掘手势识别

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

有人能帮助我吗?

回答

0

我发现它通过我自己:

[vw bringSubviewToFront:img]; 

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

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

完美的工作,希望这有助于..

+0

我改变了功能,但仍然不工作... – Bene 2013-03-14 10:21:10

+0

Okey我用UIButton替换了UIImageView,我可以看到我无法点击按钮。按钮被一个视图重叠,但我不知道它是哪个视图... – Bene 2013-03-14 10:36:00

+0

如果已经用按钮替换,然后使用此代码[yourButton addTarget:self action:@selector(methodName :) forControlEvents:UIControlEventTouchUpInside];这里是你的函数(void)mthodName:(id)sender – Dhruvik 2013-03-14 12:19:21