user2908412 user2908412 - 1 year ago 118
iOS Question

Ignoring Touch Events of embedded UIButtons when scrolling UICollectionView

I have a custom UICollectionViewCell that has a few custom UIView objects residing inside them. Each of these UIViews has a UIButton which responds to Touch Down and Touch Up Inside linked by IBActions. Basically, I want these buttons to shrink down when pressed down and spring back to their original size when let go. I can easily accomplish this with the controls and the press down and press up works. However, the problem I am facing happens when scrolling is introduced into the mix. The UICollectionView these cells are apart of is a scrolling one. If I happen to touch a button as I start my scroll, the Touch Down event is triggered as well as the scrolling event of the UICollectionView. If I recall correctly, this was never the case pre-iOS7. When a scrolling event was started, the UIButton event wasnt fired off, I think it had to do with the delaysContentTouches. This looks to be broken or changed now. It actually still works decently on iPhone, just not on iPad. If I scroll my view on iPad, with my touch starting inside the embedded UIButton, the button will shrink and the buttons action will be fired off.

So to restate the issue as plainly as I can: Is there anyway to ignore touches on embedded UIButtons when scrolling is occurring? Touches work fine when there is no scrolling triggered, I just dont want the events to fire off if the user is indeed scrolling. Is there any workaround?

If you need any more specific details, I would be happy to help you understand.

Answer Source

In a UICollectionView of mine, buttons inside of UICollectionViewCells registered TouchUpInside-taps even though the UICollectionView was still decelerating, which sounds like a similar problem to what you're having. I created a UIButton subclass that overrides beginTrackingWithTouch:withEvent and will return NO in case the UIScrollView it's contained in is decelerating or dragging.

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
    UIView *superView = self;
    while((superView = [superView superview])) {
        if ([superView isKindOfClass:UIScrollView.class]) {
            UIScrollView *scrollView = (UIScrollView *)superView;

            if (scrollView.isDecelerating || scrollView.isDragging) {
                return NO;

    return [super beginTrackingWithTouch:touch withEvent:event];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download