ios - UIButtons 动态添加到 UIScrollView 不接收触摸

标签 ios uiscrollview uibutton

在我的应用程序中,我有一个功能,允许用户打开一个包含所有应用程序页面缩略图的菜单,然后点击其中一个页面,从而进入特定页面。它基本上是一个 UIScrollView ,我向其中添加了一个 UIButton 以及每个页面的缩略图。

如果您在应用程序中间的其中一个页面上打开缩略图菜单,它的效果会非常好。然而,很多情况下,打开缩略图菜单后,90% 的 ScrollView 按钮都没有收到触摸。当用户手动到达应用程序末尾,然后打开缩略图菜单时,通常会发生这种情况。

代码如下:

@interface BookmarkManager : UIView{
    UIScrollView *thumbnailScrollView;
}
@property (strong) UIScrollView *thumbnailScrollView;


@implementation BookmarkManager

@synthesize thumbnailScrollView;

-(id)init{
    self = [super initWithFrame:CGRectMake(0, 0, 1024, 768)];
    if(self){
        [self setBackgroundColor:[UIColor clearColor]];

        thumbnailScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 120)];
        [thumbnailScrollView setBackgroundColor:[UIColor clearColor]];

        UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/thumbnail_background.png", [[NSBundle mainBundle] resourcePath] ]]];
        [self addSubview:background];

        for(int i = 0; i < totalPages; i++){

            UIButton *pageThumbnail = [UIButton buttonWithType:UIButtonTypeCustom];
            [pageThumbnail setFrame:CGRectMake(0, 0, 125, 95)];
            [pageThumbnail setImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/p%d_thumb.png", [[NSBundle mainBundle] resourcePath], i]] forState:UIControlStateNormal];

            pageThumbnail.titleLabel.text = [NSString stringWithFormat:@"%d",i];

            pageThumbnail.center = CGPointMake(20 + (i * pageThumbnail.frame.size.width) + (i * 20) +(pageThumbnail.frame.size.width/2), 60);

            [thumbnailScrollView addSubview:pageThumbnail];
            [pageThumbnail addTarget:self action:@selector(thumbnailTapped:) forControlEvents:UIControlEventTouchDown];

        }

        [self addSubview:thumbnailScrollView];
        [thumbnailScrollView setContentSize:CGSizeMake(totalPages * 125 + (20*(totalPages+1)), 120)];

    }

    return self;
}


-(void)thumbnailTapped:(id)sender{

    UIButton *thumbnailButton = sender;
    int pageNumber = [thumbnailButton.titleLabel.text intValue];

    NSLog(@"tapped thumbnail for page: %d", pageNumber);
    [bookmarkManagerDelegate jumpToPage:pageNumber];

}

我尝试将userInteractionEnabled = YES设置为pageThumbnail,尝试thumbnailScrollView BringSubviewToFront:pageThumbnail,但这不起作用......按钮总是完美地显示,问题是很多时候它们没有接收到触摸(即 NSLog 永远不会打印)...为什么会这样?

编辑: 我实现了UIScrollView的 scrollViewDidEndDecelerating,它在模拟器上被调用,但从未在设备上被调用。难道是我的按钮图片太大了?

最佳答案

我在我的应用程序中做了同样的事情。尝试使用 setBackgroundImage 而不是 setImage:。这对我有用

关于ios - UIButtons 动态添加到 UIScrollView 不接收触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420563/

相关文章:

ios - 如何基于UIImageView高度设置不同的UITableViewCell高度?

ios - 给定文本和字体,如何计算 UIButton 的宽度和高度?

iphone - 知道当UILabel的多行属性打开时在哪里拆分字符串

ios - UIImage 在 UIScrollView 上缩放时 UIButton 的位置

ios - 动态选择变量

ios - UITableView 自定义附件按钮图像在点击时消失

ios - 来自字符串的日期在 iOS 中不起作用

ios - 新foursquare field 详图

ios - UIScrollView 不完全滚动

ios - 如何动态调整scrollview高度