【问题标题】:UIButtons dynamically added to UIScrollView not receiving touchesUIButtons 动态添加到 UIScrollView 不接收触摸
【发布时间】:2012-07-10 08:34:39
【问题描述】:

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

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

代码如下:

@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,但没有奏效...按钮总是显示得非常好,问题是很多时候他们没有t 接收触摸(即 NSLog 从不打印)...为什么会这样?

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

【问题讨论】:

  • 确保内容足够大
  • 滚动视图的contentsize足够大,就像我说的,我可以很好地滚动浏览所有按钮。
  • 尝试将控制事件设置为 UIControlEventTouchUpInside。这就是我通常使用的,不确定它是否有所作为。
  • 试过了,还是一样的问题。
  • 尝试将 thumbnailScrollView 放在前面。它前面的某个地方是否有隐藏的视图?

标签: ios uiscrollview uibutton


【解决方案1】:

我在我的应用程序中做同样的事情。尝试使用 setBackgroundImage 代替 setImage:。这对我有用

【讨论】:

  • 我试过用setBackgroundImage替换setImage,还是一样的问题。
【解决方案2】:

我过去做过类似的事情,唯一的区别是您将框架设置为按钮的方式。也许这就是问题

-(void)makeIconsView:(UIScrollView*)full :(NSArray*)images{
    int row=0;
    int column=0;
    for(int i = 0; i < images.count; ++i) {

        UIImage *thumb = [images objectAtIndex:i];
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.frame = CGRectMake(column*100+14, row*90+10, 84, 80);
        [button setImage:thumb forState:UIControlStateNormal];
        [button addTarget:self 
                   action:@selector(clickImage:) 
         forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
        button.layer.shadowColor = [UIColor colorWithWhite: 0.15 alpha: 0.55].CGColor;
        button.layer.zPosition = -8;
        button.layer.shadowOffset = CGSizeMake(0, 5);
        button.layer.shadowOpacity = 1;
        button.layer.shadowRadius = 2.0;
        button.clipsToBounds = NO;
        [full addSubview:button];

        if (column == 2) {
            column = 0;
            row++;
        } else {
            column++;
        }

    }

    [full setContentSize:CGSizeMake(320, (row+1) * 90 + 10)];   
}

【讨论】:

    【解决方案3】:

    当我注意到滚动视图的滚动动画在模拟器中非常流畅但在设备(iPad 3)上很慢时,我尝试实现UIScrollViewDelegate

    在模拟器上,scrollViewWillBeginDeceleratingscrollViewDidEndDecelerating 都被调用了。在设备上,scrollViewWillBeginDecelerating 被调用,但 scrollViewDidEndDecelerating 从未被调用。

    造成这种情况的原因可能是,在设备上,滚动视图有太多要处理的内容,并且从未完成计算其内容的最终位置在哪里。因此,它会解除触摸,直到完成动画和重新定位内容。但是当内容很重时,这需要太长时间。

    所以我的解决方案是在滚动视图开始减速时立即终止动画。我只是实现了这个:

    -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
        CGPoint offset = scrollView.contentOffset;
        [scrollView setContentOffset:offset animated:NO];
    }
    

    感谢this other StackOverflow answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-09
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多