2010-10-05 120 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

相关问题