2013-06-13 61 views
0

我有循環在scrollVIew上創建圖像,我需要在點擊每個圖像時控制動作。我聽說輕拍手勢可以設置在圖像上,但我不知道如何將輕擊手勢設置爲通過循環創建的圖像。是否有可能在循環中的每個圖像上創建點擊手勢?怎麼樣?是否有可能在圖像上循環創建圖釘?

回答

0

試試這個

for(int i=0;i<20;i++) 
{ 
    UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourimage.png"]]; 
[img setTag:i]; 
img.frame= //set frame accordingly; 
    img.userInteractionEnabled = YES; 
    UITapGestureRecognizer *tap = 
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
[img addGestureRecognizer:tap]; 
[tap release]; 
[scrollView addSubView:img]; 

} 

- (void)handleTap:(UITapGestureRecognizer *)recognizer { 
    UIImageView *imageView = (UIImageView *)recognizer.view; 

     switch([imageView tag]) 
    { 
     case 1: 
      //do your work 
      break; 
     . 
     . 
     . 
     . 
     case n: 

    } 
} 
相關問題