Tuesday, June 4, 2013

UIScrollView dragging example in Objective C (iOS).


UIScrollView dragging

A Boolean value that indicates whether the user has begun scrolling the content. (read-only)

@property(nonatomic, readonly, getter=isDragging) BOOL dragging

Discussion of [UIScrollView dragging]
The value held by this property might require some time or distance of scrolling before it is set to YES.

UIScrollView dragging example.
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
        if (!self.dragging)
        {
                [[[Director sharedDirector] openGLView] touchesBegan:touches withEvent:event];
        }
       
        [super touchesBegan: touches withEvent: event];
}

-(void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
        if (!self.dragging)
        {
                [[[Director sharedDirector] openGLView] touchesEnded:touches withEvent:event];
        }
       
        [super touchesEnded: touches withEvent: event];
}

Example of [UIScrollView dragging].
- (void) addImageToView:(NSArray*) arguments {
    NSNumber* index = [arguments objectAtIndex:0];
    if (index.intValue < (currentPage - 1) ||
        index.intValue > (currentPage + 1)) {
        return;
    }
   
    if (scrollView.dragging || scrollView.decelerating) {
        [self performSelector:@selector(addImageToView:) withObject:arguments afterDelay:1];
        return;
    }

    [self addImage:[arguments objectAtIndex:1] toView:[arguments objectAtIndex:2]];
}

UIScrollView dragging example.
- (void) _gridViewDeferredTouchesBegan: (NSNumber *) indexNum
{
        if ( (self.dragging == NO) && (_flags.ignoreTouchSelect == 0) && (_pendingSelectionIndex != NSNotFound) )
                [self highlightItemAtIndex: _pendingSelectionIndex animated: NO scrollPosition: AQGridViewScrollPositionNone];
        //_pendingSelectionIndex = NSNotFound;
}

- (void) _userSelectItemAtIndex: (NSNumber *) indexNum
{
        NSUInteger index = [indexNum unsignedIntegerValue];
        [self unhighlightItemAtIndex: index animated: NO];
        if ( ([[self cellForItemAtIndex: index] isSelected]) && (self.requiresSelection == NO) )
                [self _deselectItemAtIndex: index animated: NO notifyDelegate: YES];
        else
                [self _selectItemAtIndex: index animated: NO scrollPosition: AQGridViewScrollPositionNone notifyDelegate: YES];
        _pendingSelectionIndex = NSNotFound;
}

End of UIScrollView dragging example article.