2010-10-05 52 views
0

我有一個uiscrollview,我正在顯示一些動態生成的uiimageview。現在我想要對這些圖像進行觸摸操作。我嘗試過使用touchbegn方法,但沒有被調用。請告訴我我做錯了什麼。在滾動視圖中獲取touchview上的觸摸事件

int numofRows = [arrMyPhotos count]/3; 
    if([arrMyPhotos count]%3>0) 
     numofRows++; 
    scrlPhotoList.contentSize = CGSizeMake(320, numofRows*100); 
    for(int i = 0;i<numofRows;i++) { 
     x = 10; 
     j = 0; 
     while (j<3 && k<[arrMyPhotos count]) { 
      NSString *pngFilePath = [[NSString stringWithFormat:@"%@/",docDir] stringByAppendingFormat:@"%@",[(NSDictionary*)[arrMyPhotos objectAtIndex:k] objectForKey:@"picName"]]; 
      NSLog(@"%@",pngFilePath); 
      UIImage *img = [UIImage imageWithContentsOfFile:pngFilePath]; 
      if(img!=nil) { 
       UIImageView *imgVw = [[UIImageView alloc] init]; 
       imgVw.image = [self imageByScalingAndCroppingForSize:CGRectMake(0, 0, 95, 95) image:img]; 
       imgVw.tag = k; 
       imgVw.frame = CGRectMake(x, y, 95, 95); 
       imgVw.userInteractionEnabled = TRUE; 
       imgVw.multipleTouchEnabled = TRUE; 

       [scrlPhotoList addSubview:imgVw]; 
       //k++; 
       x = x+105; 
       j++; 
      } 
      k++; 
     } 
     y = y+105; 
    } 
    [self.view addSubview:scrlPhotoList]; 



-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    //CGPoint touchLocation = [touch locationInView:self.view]; 

    if(([touch view]).tag>0) { 
     NSLog(@"%d",([touch view]).tag); 
    } 
} 
+0

你在哪裏,除了得到這件事情?你發佈的代碼與問題 – 2010-10-05 09:04:24

+0

沒有任何關係,請現在檢查它,我已經在這裏更新我的代碼 – pankaj 2010-10-05 09:05:54

回答

0

您的代碼似乎在UIControllerView中,用於處理觸摸事件,您需要擴展視圖以實現觸摸和控制器!

如果你想要什麼,是你需要具備的UIButton或支持觸摸事件的一些其他UIView的一個基本觸摸事件

+0

我使用UIViewController上的代碼,所以你的意思是我應該做一個類似的PhotoView,它應該是uiimageview的子類,有使用此代碼? – pankaj 2010-10-05 09:17:33

+0

是的,或者如果你需要像touchUpInside這樣的簡單事件,你可以在它上面放一個透明的按鈕 – 2010-10-05 09:23:55

+0

是的,我可以做到這一點,但可以有50-60張或更多的圖像,因爲它會去b照片庫,那會好嗎? ......或者我甚至可能有按鈕背景設置爲圖像 – pankaj 2010-10-05 09:27:05

相關問題