由于某种原因,我在viewController的addBook方法中初始化的按钮不会响应触摸。我分配给它的选择器永远不会触发,点击图像时也不会出现UIControlStateHighlighted图像。
在他们到达UIButton之前是否有一些拦截的东西,或者它的互动因某种程度上被我正在做的事情所禁用?
- (void)viewDidLoad {
...
_scrollView.contentSize = CGSizeMake(currentPageSize.width, currentPageSize.height);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.scrollsToTop = NO;
_scrollView.pagingEnabled = YES;
...
}
- (void)addBook {
// Make a view to anchor the UIButton
CGRect frame = CGRectMake(0, 0, currentPageSize.width, currentPageSize.height);
UIImageView* bookView = [[UIImageView alloc] initWithFrame:frame];
// Make the button
frame = CGRectMake(100, 50, 184, 157);
UIButton* button = [[UIButton alloc] initWithFrame:frame];
UIImage* bookImage = [UIImage imageNamed:kBookImage0];
// THIS SECTION NOT WORKING!
[button setBackgroundImage:bookImage forState:UIControlStateNormal];
UIImage* bookHighlight = [UIImage imageNamed:kBookImage1];
[button setBackgroundImage:bookHighlight forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(removeBook) forControlEvents:UIControlEventTouchUpInside];
[bookView addSubview:button];
[button release];
[bookView autorelease];
// Add the new view/button combo to the scrollview.
// THIS WORKS VISUALLY, BUT THE BUTTON IS UNRESPONSIVE :(
[_scrollView addSubview:bookView];
}
- (void)removeBook {
NSLog(@"in removeBook");
}
视图层次结构在Interface Builder中如下所示:
UIWindow
UINavigationController
RootViewController
UIView
UIScrollView
UIPageControl
并且一旦addBook方法运行,可能就像这样:
UIWindow
UINavigationController
RootViewController
UIView
UIScrollView
UIView
UIButton
UIPageControl
答案 0 :(得分:7)
UIScrollView
可能会抓住所有触摸事件。
也许尝试以下各项的组合:
_scrollView.delaysContentTouches = NO;
_scrollView.canCancelContentTouches = NO;
和
bookView.userInteractionEnabled = YES;
答案 1 :(得分:4)
尝试删除 [bookView autorelease]; 并按照以下方式执行操作:
// Add the new view/button combo to the scrollview.
// THIS WORKS VISUALLY, BUT THE BUTTON IS UNRESPONSIVE :(
[_scrollView addSubview:bookView];
[bookView release];
_scrollView.canCancelContentTouches = YES;
应该做的伎俩
delaysContentTouches - 是一个布尔值,用于确定滚动视图是否延迟了触摸式手势的处理。如果此属性的值为YES,则滚动视图会延迟处理触摸手势,直到它可以确定滚动是否为意图。如果值为NO,则滚动视图立即调用touchesShouldBegin:withEvent:inContentView:。默认值为YES。
canCancelContentTouches - 是一个布尔值,用于控制内容视图中的触摸是否始终导致跟踪。如果此属性的值为YES并且内容中的视图已开始跟踪触摸它的手指,并且如果用户拖动手指足以启动滚动,则视图将接收touchesCancelled:withEvent:消息并且滚动视图处理触摸滚动。如果此属性的值为NO,则在内容视图开始跟踪时,无论手指移动,滚动视图都不会滚动。