Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic multi touch behavior #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Classes/AQGridView.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ typedef enum {

// Called before selection occurs. Return a new index, or NSNotFound, to change the proposed selection.
- (NSUInteger) gridView: (AQGridView *) gridView willSelectItemAtIndex: (NSUInteger) index;
- (NSUInteger) gridView: (AQGridView *) gridView willSelectItemAtIndex: (NSUInteger) index numFingersTouch:(NSUInteger) numFingers;
- (NSUInteger) gridView: (AQGridView *) gridView willDeselectItemAtIndex: (NSUInteger) index;
// Called after the user changes the selection
- (void) gridView: (AQGridView *) gridView didSelectItemAtIndex: (NSUInteger) index;
- (void) gridView: (AQGridView *) gridView didSelectItemAtIndex: (NSUInteger) index numFingersTouch:(NSUInteger)numFingers;
- (void) gridView: (AQGridView *) gridView didDeselectItemAtIndex: (NSUInteger) index;

// NOT YET IMPLEMENTED
Expand Down Expand Up @@ -124,7 +126,7 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;

UIView * _headerView;
UIView * _footerView;
struct
{
unsigned resizesCellWidths:1;
Expand All @@ -146,8 +148,10 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;

unsigned delegateWillDisplayCell:1;
unsigned delegateWillSelectItem:1;
unsigned delegateWillSelectItemMultiTouch:1;
unsigned delegateWillDeselectItem:1;
unsigned delegateDidSelectItem:1;
unsigned delegateDidSelectItemMultiTouch:1;
unsigned delegateDidDeselectItem:1;
unsigned delegateGestureRecognizerActivated:1;
unsigned delegateAdjustGridCellFrame:1;
Expand Down
56 changes: 47 additions & 9 deletions Classes/AQGridView.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@
#import <objc/objc.h>
#import <objc/runtime.h>

// Lightweight object class for touch selection parameters
@interface UserSelectItemIndexParams : NSObject
{
NSUInteger _indexNum;
NSUInteger _numFingers;
};

@property (nonatomic, assign) NSUInteger indexNum;
@property (nonatomic, assign) NSUInteger numFingers;
@end

@implementation UserSelectItemIndexParams

@synthesize indexNum = _indexNum;
@synthesize numFingers = _numFingers;

@end


NSString * const AQGridViewSelectionDidChangeNotification = @"AQGridViewSelectionDidChangeNotification";

@interface AQGridView (AQCellGridMath)
Expand All @@ -71,6 +90,7 @@ @interface AQGridView ()
@property (nonatomic, copy) NSIndexSet * animatingIndices;
@end


@implementation AQGridView

@synthesize dataSource=_dataSource, backgroundView=_backgroundView, separatorColor=_separatorColor, animatingCells=_animatingCells, animatingIndices=_animatingIndices;
Expand Down Expand Up @@ -158,8 +178,10 @@ - (void) setDelegate: (id<AQGridViewDelegate>) obj

_flags.delegateWillDisplayCell = [obj respondsToSelector: @selector(gridView:willDisplayCell:forItemAtIndex:)];
_flags.delegateWillSelectItem = [obj respondsToSelector: @selector(gridView:willSelectItemAtIndex:)];
_flags.delegateWillSelectItemMultiTouch = [obj respondsToSelector: @selector(gridView:willSelectItemAtIndex:numFingersTouch:)];
_flags.delegateWillDeselectItem = [obj respondsToSelector: @selector(gridView:willDeselectItemAtIndex:)];
_flags.delegateDidSelectItem = [obj respondsToSelector: @selector(gridView:didSelectItemAtIndex:)];
_flags.delegateDidSelectItemMultiTouch = [obj respondsToSelector: @selector(gridView:didSelectItemAtIndex:numFingersTouch:)];
_flags.delegateDidDeselectItem = [obj respondsToSelector: @selector(gridView:didDeselectItemAtIndex:)];
_flags.delegateGestureRecognizerActivated = [obj respondsToSelector: @selector(gridView:gestureRecognizer:activatedForItemAtIndex:)];
_flags.delegateAdjustGridCellFrame = [obj respondsToSelector: @selector(gridView:adjustCellFrame:withinGridCellFrame:)];
Expand Down Expand Up @@ -1106,6 +1128,7 @@ - (void) _deselectItemAtIndex: (NSUInteger) index animated: (BOOL) animated noti

- (void) _selectItemAtIndex: (NSUInteger) index animated: (BOOL) animated
scrollPosition: (AQGridViewScrollPosition) position notifyDelegate: (BOOL) notifyDelegate
numFingersTouch: (NSUInteger) numFingers
{
if ( _selectedIndex == index )
return; // already selected this item
Expand All @@ -1118,7 +1141,11 @@ - (void) _selectItemAtIndex: (NSUInteger) index animated: (BOOL) animated

if ( notifyDelegate && _flags.delegateWillSelectItem )
index = [self.delegate gridView: self willSelectItemAtIndex: index];


if ( notifyDelegate && _flags.delegateWillSelectItemMultiTouch )
index = [self.delegate gridView: self willSelectItemAtIndex: index
numFingersTouch:numFingers];

_selectedIndex = index;
[[self cellForItemAtIndex: index] setSelected: YES animated: animated];

Expand All @@ -1133,6 +1160,9 @@ - (void) _selectItemAtIndex: (NSUInteger) index animated: (BOOL) animated

if ( notifyDelegate && _flags.delegateDidSelectItem )
[self.delegate gridView: self didSelectItemAtIndex: index];

if ( notifyDelegate && _flags.delegateDidSelectItemMultiTouch )
[self.delegate gridView: self didSelectItemAtIndex: index numFingersTouch:numFingers];

// ensure that the selected item is no longer marked as just 'highlighted' (that's an intermediary state)
[_highlightedIndices removeIndex: index];
Expand All @@ -1141,7 +1171,8 @@ - (void) _selectItemAtIndex: (NSUInteger) index animated: (BOOL) animated
- (void) selectItemAtIndex: (NSUInteger) index animated: (BOOL) animated
scrollPosition: (AQGridViewScrollPosition) scrollPosition
{
[self _selectItemAtIndex: index animated: animated scrollPosition: scrollPosition notifyDelegate: NO];
[self _selectItemAtIndex: index animated: animated scrollPosition: scrollPosition notifyDelegate: NO
numFingersTouch: 1];
}

- (void) deselectItemAtIndex: (NSUInteger) index animated: (BOOL) animated
Expand Down Expand Up @@ -1246,14 +1277,16 @@ - (void) _gridViewDeferredTouchesBegan: (NSNumber *) indexNum
//_pendingSelectionIndex = NSNotFound;
}

- (void) _userSelectItemAtIndex: (NSNumber *) indexNum
- (void) _userSelectItemAtIndex: (UserSelectItemIndexParams*) params
{
NSUInteger index = [indexNum unsignedIntegerValue];
NSUInteger index = params.indexNum;
NSUInteger numFingersCount = params.numFingers;
[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];
[self _selectItemAtIndex: index animated: NO scrollPosition: AQGridViewScrollPositionNone notifyDelegate: YES
numFingersTouch: numFingersCount];
_pendingSelectionIndex = NSNotFound;
}

Expand Down Expand Up @@ -1297,7 +1330,7 @@ - (BOOL) _gestureRecognizerIsHandlingTouches: (NSSet *) touches
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
_flags.ignoreTouchSelect = ([self isDragging] ? 1 : 0);
UITouch * touch = [touches anyObject];
_touchBeganPosition = [touch locationInView: nil];
if ( (touch != nil) && (_pendingSelectionIndex == NSNotFound) )
Expand Down Expand Up @@ -1365,7 +1398,7 @@ - (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
[[self class] cancelPreviousPerformRequestsWithTarget: self
[[self class] cancelPreviousPerformRequestsWithTarget: self
selector: @selector(_gridViewDeferredTouchesBegan:)
object: nil];

Expand Down Expand Up @@ -1405,10 +1438,15 @@ - (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
if ( _flags.allowsSelection == 0 )
break;

NSSet *touchEventSet = [event allTouches];

// run this on the next runloop tick
UserSelectItemIndexParams* selectorParams = [[[UserSelectItemIndexParams alloc] init] autorelease];
selectorParams.indexNum = _pendingSelectionIndex;
selectorParams.numFingers = [touchEventSet count];
[self performSelector: @selector(_userSelectItemAtIndex:)
withObject: [NSNumber numberWithUnsignedInteger: _pendingSelectionIndex]
afterDelay: 0.0];
withObject: selectorParams
afterDelay:0.0];

[hitView release];

Expand Down