我在UIScrollview中遇到TTImagViews问题。我搜索得很高,但很难找到解决办法。有趣的是,点击手势适用于scrollview中的最后一个TTImageview。例如,我有10个图像,用户可以滚动,触摸手势仅适用于第2页上的第10个图像,或者最后一个图像。这是我的代码;有什么建议吗?
UIScrollView *imageScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(70, postMessageLabel.frame.size.height+10, 250, 78)];
[imageScroll setContentSize:CGSizeMake(70 * ([[images objectAtIndex:0]count])+10,64)];
int startAtX=5;
for(int i=0;i<[[images objectAtIndex:0]count];i++){
if([[GlobalFunctions sharedGlobalFunctions] isValidURL:[[images objectAtIndex:0] objectAtIndex:i]]){
TTImageView *imageView=[[TTImageView alloc] initWithFrame:CGRectMake(startAtX, 5, 64, 64)] ;
imageView.userInteractionEnabled=YES;
[imageView addGestureRecognizer:thumbnailTap];
imageView.urlPath=[[images objectAtIndex:0] objectAtIndex:i];
imageView.autoresizesToImage=NO;
imageView.defaultImage=nil;
imageView.delegate=self;
[imageView setBackgroundColor:[UIColor blackColor]];
[imageScroll addSubview:imageView];
[imageView release];
}
startAtX+=70;
}
[imageScroll setBounces:YES];
[imageScroll setDelaysContentTouches:YES];
[imageScroll setCanCancelContentTouches:NO];
[self.view addSubview:imageScroll];
[imageScroll release];
是的,如果在uiscrollview中只有一个ttimageview,那么点击手势就可以完美地运行。我不知道为什么!
答案 0 :(得分:0)
在UIView中添加图像视图,然后向该UIView添加点击手势。这对我有用,也适合你。请尝试以下方法:
UIScrollView *imageScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(70, postMessageLabel.frame.size.height+10, 250, 78)];
[imageScroll setContentSize:CGSizeMake(70 * ([[images objectAtIndex:0]count])+10,64)];
int startAtX=5;
for(int i=0;i<[[images objectAtIndex:0]count];i++){
if([[GlobalFunctions sharedGlobalFunctions] isValidURL:[[images objectAtIndex:0] objectAtIndex:i]]){
TTImageView *imageView=[[TTImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)] ;
imageView.userInteractionEnabled=YES;
[imageView addGestureRecognizer:thumbnailTap];
imageView.urlPath=[[images objectAtIndex:0] objectAtIndex:i];
imageView.autoresizesToImage=NO;
imageView.defaultImage=nil;
imageView.delegate=self;
[imageView setBackgroundColor:[UIColor blackColor]];
UIView *viewWithImg=[[UIView alloc] initWithFrame:CGRectMake(startAtX, 5, 64, 64)];
[viewWithImg addSubview:imgView];
viewWithImg.tag=i;
UITapGestureRecognizer *tapGesture2 =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured2:)];
tapGesture2.numberOfTapsRequired = 1;
tapGesture2.numberOfTouchesRequired=1;
[viewWithImg addGestureRecognizer:tapGesture2];
[imageScroll addSubview: viewWithImg];
[imageView release];
}
startAtX+=70;
}
[imageScroll setBounces:YES];
[imageScroll setDelaysContentTouches:YES];
[imageScroll setCanCancelContentTouches:NO];
[self.view addSubview:imageScroll];
[imageScroll release];
和singleTapGestureCaptured2方法
-(void)singleTapGestureCaptured2:(UITapGestureRecognizer *)gesture
{
NSLog(@"image tag or arr index from images array: %d",[[gesture view] tag]);
}
答案 1 :(得分:0)
好吧,看起来我的代码没有任何问题;唯一的区别是我如何附加手势识别器。即使我修好了,我也不确定为什么会出错。这就是发生的事情
第2步;好像是问题所在。当我为每个图像声明一个手势识别器并在分配后释放它时,问题就消失了。
技术上对每个图像
---Create a gesture recognizer
|-Attach it to the TTImageView
---Release the gesture recognizer
解决了这个问题。但我仍然很好奇,为什么手势识别器在分配给单个图像时工作,当我将其分配给多个图像视图时失败。
感谢阿卜杜拉的努力,但是你的解决方案让我无处可去。