iPhone: adding button to scrollview makes button i

2020-04-11 06:17发布

问题:

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.

Is there something intercepting touches before they get to the UIButton, or is its interactivity somehow disabled by what I'm doing to it?

- (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");
}

The view hierarchy looks like this in Interface Builder:

UIWindow
UINavigationController
    RootViewController
        UIView
            UIScrollView
            UIPageControl

and presumably like this once the addBook method runs:

UIWindow
UINavigationController
    RootViewController
        UIView
            UIScrollView
                UIView
                    UIButton
            UIPageControl

回答1:

THe UIScrollView might be catching all the touch events.

Maybe try a combination of the following:

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

and

bookView.userInteractionEnabled = YES;


回答2:

try to remove the [bookView autorelease]; and do it like this:

// 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; should do the trick

delaysContentTouches - is a Boolean value that determines whether the scroll view delays the handling of touch-down gestures. If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is NO , the scroll view immediately calls touchesShouldBegin:withEvent:inContentView:. The default value is YES.

canCancelContentTouches - is a Boolean value that controls whether touches in the content view always lead to tracking. If the value of this property is YES and a view in the content has begun tracking a finger touching it, and if the user drags the finger enough to initiate a scroll, the view receives a touchesCancelled:withEvent: message and the scroll view handles the touch as a scroll. If the value of this property is NO, the scroll view does not scroll regardless of finger movement once the content view starts tracking.