【问题标题】:Problem when using UIImageView's or UIButton's inside a UIScrollView在 UIScrollView 中使用 UIImageView 或 UIButton 时出现问题
【发布时间】:2010-08-23 01:27:10
【问题描述】:

我正在使用 UIScrollView 来保存大小为 80x80 的不同数量的图像,当用户点击一个时,我希望它启动到一个模态视图中,显示它全屏等。 我遇到的问题是检测滚动视图内图像的触摸。到目前为止,我已经尝试了两种方法,但每种方法都有问题。我同时发布了两种方式,但我只需要其中一种方式的答案。

我有一个图像数组并循环遍历它,这是我尝试为每个图像使用 UIImageView 的第一种方法:

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;
}

这导致的问题是,在这个循环之后,我必须在 UIScrollView 上将 userInteractionEnabled 设置为 NO,这样我才能覆盖 touchesBegan 并检测触摸,但是这样做当然会禁用滚动,因此用户只能看到前 6 个图像左右.

有没有办法可以重新启用滚动? photoScroller.scrollEnabled = YES;由于用户交互已被禁用,因此无效。

...

我尝试的第二种方法是为每个图像使用一个 UIButton,在这种情况下循环的代码如下:

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;
}

现在这段代码几乎可以完美运行,唯一的问题是用户只有在触摸空白时才能滚动。

有什么办法可以做到,如果用户在 UIButton 上拖动它仍然会滚动 UIScrollView?

【问题讨论】:

    标签: iphone cocoa-touch uiscrollview


    【解决方案1】:

    UIScrollView 有一个名为delaysContentTouches 的属性。默认情况下设置为YES,这会导致您想要的行为:拖动始终有效,即使您点击例如UIButton

    所以,如果您没有看到该行为,那么您可能已将 delaysContentTouches 设置为 NO

    此外,与其在滚动视图中使用 UIImageViews 并覆盖他们的 touchedBegan: 和朋友,不如简单地使用 UIButtons 可能要容易得多

    您可以简单地将按钮的图像设置为您想要的任何内容,而不必对它们进行子类化。只需使用addTarget: 使按钮在单击时回调到您的视图控制器。 (这与使用 Interface Builder 连接到 IBAction 相同)

    【讨论】:

      【解决方案2】:

      最终证明这不是一个问题,我使用了UIButtons 以防有人想知道,一切似乎都按预期工作,正如我想要的那样。

      我在滚动 UIScrollView 时遇到的问题似乎在更改了一些代码后自行修复,但不完全确定是什么导致了问题 - 在实际设备上使用时甚至都不是问题。

      【讨论】:

        【解决方案3】:

        过去我在使用添加到滚动视图的自定义视图上的按钮时遇到了一些问题。 @selectors 似乎根本没有响应。更具体地说,我正在创建一个分页滚动视图,其中的 UIView 嵌入了 uiwebview 和一些按钮。我最终从嵌入式视图中删除了按钮并将它们放在父控制器中。我现在可以接收触摸事件 - 我想在 uiscrollview 和 uiwebview 之间有一些奇怪的触摸事件集群。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-04
          • 2020-11-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多