Tuesday, June 4, 2013

UIScrollView delaysContentTouches example in Objective C (iOS).

UIScrollView delaysContentTouches

A Boolean value that determines whether the scroll view delays the handling of touch-down gestures.

@property(nonatomic) BOOL delaysContentTouches

Discussion of [UIScrollView delaysContentTouches]
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.

See the class description for a fuller discussion.

UIScrollView delaysContentTouches example.
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event

    UIView* result = [super hitTest:point withEvent:event];

    NSString *viewDescription = [result.superview class].description;
    NSRange range = [viewDescription rangeOfString : @"UIPicker"];

    if (range.location != NSNotFound)
        NSLog(@"Cancel touch on ScrollView");
        self.canCancelContentTouches = NO; 
        self.delaysContentTouches = NO;
        self.scrollEnabled = NO;
        self.canCancelContentTouches = YES;
        self.delaysContentTouches = YES;
        self.scrollEnabled = YES;
    return result;

Example of [UIScrollView delaysContentTouches].
- (id)initWithFrame:(CGRect)frame
    self = [super initWithFrame:frame];
    if (self) {


        [cell.xButton addTarget:self action:@selector(xButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

        self.delaysContentTouches = YES;

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
        [self addGestureRecognizer:tapGestureRecognizer];
        tapGestureRecognizer.delegate = self;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    UIView* touchedView = [touch view];
    if([touchedView isKindOfClass:[UIButton class]]) {

       return NO;

    return YES;

UIScrollView delaysContentTouches example.
//init scrolling area
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 480, 480)];
scrollView.contentSize = CGSizeMake(480, 1000);
scrollView.bounces = NO;
scrollView.delaysContentTouches = YES;

//create background image
UIImageView *rowsBackground = [[UIImageView alloc] initWithImage:[self scaleAndRotateImage:[UIImage imageNamed:@"mylongbackground.png"]]];
rowsBackground.userInteractionEnabled = YES;

//create button
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn.frame = CGRectMake(100, 850, 150, 150);
btn.bounds = CGRectMake(0, 0, 150.0, 150.0);
[btn setImage:[self scaleAndRotateImage:[UIImage imageNamed:@"basicbutton.png"]] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

//add "stuff" to scrolling area
[scrollView addSubview:rowsBackground];
[scrollView addSubview:btn];

//add scrolling area to cocos2d
//this is just a UIWindow
[[[Director sharedDirector] openGLView] addSubview:scrollView];

[rowsBackground release];
[btn release];
[scrollView release];

End of UIScrollView delaysContentTouches example article.