2013-12-17 50 views
-2

我無法用輕擊手勢創建ImageView列表。當我創建手勢時,選擇器功能不被調用。當我只創建一個imageView函數時被調用。任何想法爲什麼?這是大滾動視圖的所有子視圖。使用輕擊手勢識別器添加圖像視圖列表時不會調用選擇器功能

這是我的代碼:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(imageTaped:)]; 
singleTap.numberOfTapsRequired = 1; 
singleTap.numberOfTouchesRequired = 1; 
for(int k=0;k<MyList.count;k++){ 
    for(int i=0;i<listSize;i++){ 
     UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)]; 
     clickForDetail.backgroundColor = [UIColor clearColor]; 
     clickForDetail.tag = tag; 
     clickForDetail.userInteractionEnabled = YES; 
     [clickForDetail addGestureRecognizer:singleTap]; 

     [myScroll addSubview:clickForDetail]; 
     tag++; 
    } 
} 

和選擇功能:

-(void)imageTaped: (UITapGestureRecognizer *)recognizer 
{ 
    NSLog(@"single Tap on imageview"); 
} 

是否有可能以某種方式來獲得點擊,一個ImageView的吊牌?

回答

2

你需要把不同的點觸手勢爲每個視圖對象

for(int k=0;k<MyList.count;k++){ 
    for(int i=0;i<listSize;i++){ 
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(imageTaped:)]; 
singleTap.numberOfTapsRequired = 1; 
singleTap.numberOfTouchesRequired = 1; 
     UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)]; 
     clickForDetail.backgroundColor = [UIColor clearColor]; 
     clickForDetail.tag = tag; 
     clickForDetail.userInteractionEnabled = YES; 
     [clickForDetail addGestureRecognizer:singleTap]; 

     [epgScroll addSubview:clickForDetail]; 
     tag++; 
    } 
} 


-(void)imageTaped: (UITapGestureRecognizer *)recognizer 
{ 
    NSLog(@"single Tap on imageview"); 
UIImageView *selectedTextView = (UIImageView *)recognizer.view; 
} 
+0

是否有可能檢測哪個圖像視圖是由它的標籤點擊? – lugonja

+0

@lugonja我已更新回答 –

+0

謝謝!這就是我需要的。 – lugonja

相關問題