【问题标题】:iPhone: adding button to scrollview makes button inaccessible to interactioniPhone:向滚动视图添加按钮使按钮无法交互
【发布时间】:2011-01-22 05:26:04
【问题描述】:

由于某种原因,在我的 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

【问题讨论】:

    标签: iphone objective-c uiscrollview uibutton


    【解决方案1】:

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

    也许可以尝试以下组合:

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

    bookView.userInteractionEnabled = YES;
    

    【讨论】:

    • userInteractionEnabled 对于我添加 UIButton 作为子视图的视图来说是 NO。这是原因,而不是其他 2 个设置。
    【解决方案2】:

    尝试删除 [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,则一旦内容视图开始跟踪,无论手指移动如何,滚动视图都不会滚动。

    【讨论】:

    • 糟糕。是的,我没有正确使用自动释放......浪费。谢谢,SorinA。
    猜你喜欢
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多