2013-03-13 94 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