gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:21:01 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个功能,允许用户打开一个包含所有应用程序页面缩略图的菜单,然后点击一个页面,然后点击一个特定页面。它基本上是一个 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's scrollViewDidEndDecelerating,它在模拟器上被调用但从未在设备上被调用。会不会是我按钮的图片太大了?

最佳答案

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com