iPhone:向scrollview添加按钮使按钮无法进行交互

clo*_*ach 3 iphone objective-c uibutton uiscrollview

出于某种原因,在我的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");
}
Run Code Online (Sandbox Code Playgroud)

视图层次结构在Interface Builder中如下所示:

UIWindow
UINavigationController
    RootViewController
        UIView
            UIScrollView
            UIPageControl
Run Code Online (Sandbox Code Playgroud)

并且一旦addBook方法运行,大概就像这样:

UIWindow
UINavigationController
    RootViewController
        UIView
            UIScrollView
                UIView
                    UIButton
            UIPageControl
Run Code Online (Sandbox Code Playgroud)

MrM*_*age 7

UIScrollView可能是所有的触摸事件.

也许尝试以下组合:

_scrollView.delaysContentTouches = NO;
_scrollView.canCancelContentTouches = NO;
Run Code Online (Sandbox Code Playgroud)

bookView.userInteractionEnabled = YES;
Run Code Online (Sandbox Code Playgroud)