Iphone 使用UIImageView时出现问题';s或UIButton';它在UIScrollView中
我使用UIScrollView来保存大小为80x80的不同数量的图像,当用户点击一个图像时,我希望它进入一个模式视图,显示全屏等等。 我遇到的问题是检测scrollview中图像的触摸。到目前为止,我已经尝试了两种方法,但每种方法都有一个问题。我张贴两种方式,但我只需要一个答案,其中之一 我有一个图像数组并在其中循环,这是我第一次尝试为每个图像使用UIImageView:Iphone 使用UIImageView时出现问题';s或UIButton';它在UIScrollView中,iphone,cocoa-touch,uiscrollview,Iphone,Cocoa Touch,Uiscrollview,我使用UIScrollView来保存大小为80x80的不同数量的图像,当用户点击一个图像时,我希望它进入一个模式视图,显示全屏等等。 我遇到的问题是检测scrollview中图像的触摸。到目前为止,我已经尝试了两种方法,但每种方法都有一个问题。我张贴两种方式,但我只需要一个答案,其中之一 我有一个图像数组并在其中循环,这是我第一次尝试为每个图像使用UIImageView: for (i=0; i<[imageArray count]; i++) { // get the image
for (i=0; i<[imageArray count]; i++) {
// get the image
UIImage *theImage = [imageArray objectAtIndex:i];
// figure out the size for scaled down version
float aspectRatio = maxImageHeight / theImage.size.height;
float thumbWidth = theImage.size.width * aspectRatio;
float thumbHeight = maxImageHeight;
lastX = lastX+10;
// Scale the image down
UIImage *thisImage = [theImage imageByScalingProportionallyToSize:CGSizeMake(thumbWidth, thumbHeight)];
// Create an 80x80 UIImageView to hold the image, positioned 10px from edge of the previous one
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(lastX, 10, 80, 80)];
image.clipsToBounds = YES;
image.contentMode = UIViewContentModeTopLeft;
image.image = thisImage;
image.userInteractionEnabled = YES;
// add to the scroller
[photoScroller addSubview:image];
// release
[image release];
// set variables ready for the next one
//lastWidth = thisImage.size.width;
lastWidth = 80; // we're using square images so set to 80 rather than the width of the image
lastX = lastX+lastWidth;
}
for(i=0;i的UIScrollView
有一个名为delaysContentTouches
的属性。默认情况下,该属性设置为YES
,这会导致您想要的行为:拖动始终有效,即使您点击例如UIButton
因此,如果您没有看到这种行为,那么您可能已经将delaysContentTouches
设置为NO
此外,与其在scrollview中使用UIImageView
s,并覆盖他们的TouchedBegind:
和friends,不如简单地使用UIButton
s
您可以简单地将按钮的图像设置为您想要的任何图像,并且不必对其进行子类化。只需使用addTarget:
使按钮在单击时回调到您的viewcontroller。(这与使用Interface Builder连接到iAction
)最终证明这不是什么问题,我使用了ui按钮
s,以防有人怀疑,一切似乎都按照预期和我希望的那样工作
我在修改一些代码后,滚动UIScrollView
时遇到的问题似乎得到了解决,但不完全确定是什么导致了问题-在实际设备上使用时,这甚至不是问题。我过去在滚动视图中添加自定义视图上的按钮时遇到了一些问题。@selector似乎不起作用完全响应。更具体地说,我创建了一个页面滚动视图,其中UIView嵌入了uiwebview和一些按钮。我最终从嵌入的视图中删除了按钮,并将它们放置在父控制器中。我现在可以接收触摸事件-我想在uiscrollview和uiwebv之间有一些奇怪的触摸事件集群视图
for (i=0; i<[imageArray count]; i++) {
// get the image
UIImage *theImage = [imageArray objectAtIndex:i];
// figure out the size for scaled down version
float aspectRatio = maxImageHeight / theImage.size.height;
float thumbWidth = theImage.size.width * aspectRatio;
float thumbHeight = maxImageHeight;
lastX = lastX+10;
// Scale the image down
UIImage *thisImage = [theImage imageByScalingProportionallyToSize:CGSizeMake(thumbWidth, thumbHeight)];
// Create an 80x80 UIButton to hold the image, positioned 10px from the edge of the previous one
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(lastX, 10, 80, 80)];
button.clipsToBounds = YES;
button.contentMode = UIViewContentModeTopLeft;
[button setImage:thisImage forState:UIControlStateNormal];
[button setImage:thisImage forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(tapImage:) forControlEvents:UIControlEventTouchUpInside];
// add to the scroller;
[photoScroller addSubview:button];
// release
[button release];
// set variables ready for the next one
//lastWidth = thisImage.size.width;
lastWidth = 80; // we're using square images so set to 80 rather than the width of the image
lastX = lastX+lastWidth;
}