iPhone:向scrollview添加按钮使按钮无法进行交互 [英] iPhone: adding button to scrollview makes button inaccessible to interaction

查看:96
本文介绍了iPhone:向scrollview添加按钮使按钮无法进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

也许尝试以下组合:

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

bookView.userInteractionEnabled = YES;

这篇关于iPhone:向scrollview添加按钮使按钮无法进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆