在UIScrollView中使用UIImageView或UIButton时出现问题 [英] Problem when using UIImageView's or UIButton's inside a UIScrollView

查看:34
本文介绍了在UIScrollView中使用UIImageView或UIButton时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a UIScrollView to hold a varying number of images sized at 80x80, and when a user taps one I want it to launch into a modal view showing it fullscreen etc. The problem I'm having is detecting touches on the images inside the scrollview. I've tried two ways so far, but each one has an issue. I'm posting both ways but I only need an answer to one of them.

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

I have an array of images and loop through it, this is the first method I tried using a UIImageView for each one:

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张图像左右

The problem this causes is that after this loop I have to set userInteractionEnabled to NO on the UIScrollView so I can override touchesBegan and detect touches, but doing this of course disables scrolling so the user can only see the first 6 images or so.

有没有一种方法可以使我重新启用滚动功能?photoScroller.scrollEnabled =是;由于用户互动已被禁用,因此无效.

Is there a way I can re-enable scrolling so to speak? photoScroller.scrollEnabled = YES; has no effect since user interaction has been disabled.

...

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

The second way I tried was using a UIButton for each image, the code for the loop in this case is as follows:

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

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

Now this code works almost perfectly, the only problem is that the user can only scroll if they touch in whitespace.

有没有办法做到这一点,因此,如果用户在UIButton上拖动,它仍会滚动UIScrollView?

Is there a way I can make it so if a user is dragging on the UIButton it will still scroll the UIScrollView?

推荐答案

最后证明是没有问题的,我使用了 UIButton s,以防万一有人在想,所有似乎可以按预期的方式工作,并且按我的意愿进行.

Turned out to be a bit of a non-issue in the end, I used UIButtons in case anyone was wondering, all seems to work as expected and as I wanted it to.

在滚动一些代码后,我在滚动 UIScrollView 时遇到的问题似乎已经解决,无法完全确定导致问题的原因-在实际环境中使用时,这甚至不是问题设备.

The issue I had with scrolling the UIScrollView appeared to fix itself after altering some of the code, not entirely sure what was causing the problem - it wasn't even an issue when used on an actual device.

这篇关于在UIScrollView中使用UIImageView或UIButton时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆