iPhone:向滚动视图添加按钮使按钮无法交互

编程入门 行业动态 更新时间:2024-10-27 14:26:38
本文介绍了iPhone:向滚动视图添加按钮使按钮无法交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于某种原因,在我的 viewController 的 addBook 方法中初始化的按钮不会响应触摸.我分配给它的选择器永远不会触发,UIControlStateHighlighted 图像也不会在点击图像时出现.

For some reason, the button initialized in the addBook method of my viewController won't respond to touches. The selector I've assigned to it never triggers, nor does the UIControlStateHighlighted image ever appear when tapping on the image.

在触摸到 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 中的视图层次结构如下所示:

The view hierarchy looks like this in Interface Builder:

UIWindow UINavigationController RootViewController UIView UIScrollView UIPageControl

一旦 addBook 方法运行,大概就是这样:

and presumably like this once the addBook method runs:

UIWindow UINavigationController RootViewController UIView UIScrollView UIView UIButton UIPageControl

推荐答案

UIScrollView 可能正在捕获所有的触摸事件.

THe UIScrollView might be catching all the touch events.

也许可以尝试以下组合:

Maybe try a combination of the following:

_scrollView.delaysContentTouches = NO; _scrollView.canCancelContentTouches = NO;

bookView.userInteractionEnabled = YES;

更多推荐

iPhone:向滚动视图添加按钮使按钮无法交互

本文发布于:2023-05-29 15:06:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/347267.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file 'D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!