【发布时间】: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