在我的应用程序中,我具有允许用户打开包含所有应用程序页面缩略图的菜单的功能,然后点击其中一个,从而进入特定页面。它基本上是一个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
,但是没有用......按钮总是显示得很好,问题是有很多时候他们没有收到触摸(即 NSLog 从不打印).. . 为什么会这样?
编辑:我实现UIScrollView's
scrollViewDidEndDecelerating
了,它在模拟器上被调用,但从未在设备上被调用。会不会是我的按钮图片太大了?