Wednesday, June 5, 2013

UIScrollView touchesShouldCancelInContentView example in Objective C (iOS).


UIScrollView touchesShouldCancelInContentView

Returns whether to cancel touches related to the content subview and start dragging.

- (BOOL)touchesShouldCancelInContentView:(UIView *)view

Parameters
view
The view object in the content that is being touched.

Return Value of [UIScrollView touchesShouldCancelInContentView]
YES to cancel further touch messages to view, NO to have view continue to receive those messages. The default returned value is YES if view is not a UIControl object; otherwise, it returns NO.

Discussion of [UIScrollView touchesShouldCancelInContentView]
The scroll view calls this method just after it starts sending tracking messages to the content view. If it receives NO from this method, it stops dragging and forwards the touch events to the content subview. The scroll view does not call this method if the value of the canCancelContentTouches property is NO.

UIScrollView touchesShouldCancelInContentView example.
myCustomScrollView.h

@interface myCustomScrollView : UIScrollView  {

}

@end
and myCustomScrollView.m

@implementation myCustomScrollView

    - (BOOL)touchesShouldCancelInContentView:(UIView *)view
    {
        NSLog(@"touchesShouldCancelInContentView");

        if (view.tag == 99)
            return NO;
        else
            return YES;
    }

Example of [UIScrollView touchesShouldCancelInContentView].
#import "scrollViewWithButtons.h"

@implementation scrollViewWithButtons

- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    return ![view isKindOfClass:[UISlider class]];
}

@end

UIScrollView touchesShouldCancelInContentView example.
overloaded the following methods of th UIScrollview:

-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    if ([view isKindOfClass:[self class]])
    {
        return YES; //if there are two nested custom scrollviews 
    }

if ([view isKindOfClass:[UIButton class]] || [view isKindOfClass:[CustomSegmentedControl class]] || [[view superview] isKindOfClass:[CustomSegmentedControl class]]) {
        return YES;
    }

    if ([view isKindOfClass:[UIControl class]]) {
        return NO;
    }

    return YES;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.dragging) {
        for (UIView *sv in self.subviews) {
            if ([sv isKindOfClass:[CustomSegmentedControl class]])
            {   //if there is more than one add a tag or tomething
                [(CustomSegmentedControl *)sv setTouchProcessed:YES];
                [sv touchesBegan:touches withEvent:event];
                return;
             }
        }
    }
}

End of UIScrollView touchesShouldCancelInContentView example article.