In my app, I have feature that allows the user to open a menu with thumbnails of all the app's pages, then tap on one and thus to a particular page. It's basically a UIScrollView
to which I add a UIButton
with a thumbnail image of each page.
The thumbnail menu works perfectly well if you open it on one of the pages in the middle of the app. However, there are many cases where, after opening the thumbnail menu, 90% of the scrollview's buttons don't receive touches. It usually happens when the user manually reaches the end of the app, then opens the thumbnail menu.
Here's the code:
@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];
}
I've tried setting userInteractionEnabled = YES
to pageThumbnail
, tried thumbnailScrollView bringSubviewToFront:pageThumbnail
, but that didn't work... The buttons are always perfectly well displayed, the problem is that there are many times where they don't receive touches (i.e. that NSLog never prints)... Why could that be?
EDIT:
I implemented UIScrollView's
scrollViewDidEndDecelerating
, it gets called on the Simulator but never gets called on the device. Could it be that my button's images are too big?