Skip to content

Commit

Permalink
Added options enableEditOnLongPress, disableEditOnEmptySpaceTap; adde…
Browse files Browse the repository at this point in the history
…d -changedEdit: callback; added cell highlighting
  • Loading branch information
futuretap committed May 3, 2012
1 parent 3f61a66 commit 0135d17
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 29 deletions.
4 changes: 4 additions & 0 deletions GMGridView/GMGridView.h
Expand Up @@ -84,6 +84,8 @@ typedef enum
@property (nonatomic) UIEdgeInsets minEdgeInsets; // Default is (5, 5, 5, 5) @property (nonatomic) UIEdgeInsets minEdgeInsets; // Default is (5, 5, 5, 5)
@property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.2; if set to 0, the view wont be scrollable @property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.2; if set to 0, the view wont be scrollable
@property (nonatomic) BOOL showFullSizeViewWithAlphaWhenTransforming; // Default is YES - not working right now @property (nonatomic) BOOL showFullSizeViewWithAlphaWhenTransforming; // Default is YES - not working right now
@property (nonatomic) BOOL enableEditOnLongPress; // Default is NO
@property (nonatomic) BOOL disableEditOnEmptySpaceTap; // Default is NO


@property (nonatomic, readonly) UIScrollView *scrollView __attribute__((deprecated)); // The grid now inherits directly from UIScrollView @property (nonatomic, readonly) UIScrollView *scrollView __attribute__((deprecated)); // The grid now inherits directly from UIScrollView


Expand Down Expand Up @@ -147,6 +149,8 @@ typedef enum
// This method wont delete the cell automatically. Call the delete method of the gridView when appropriate. // This method wont delete the cell automatically. Call the delete method of the gridView when appropriate.
- (void)GMGridView:(GMGridView *)gridView processDeleteActionForItemAtIndex:(NSInteger)index; - (void)GMGridView:(GMGridView *)gridView processDeleteActionForItemAtIndex:(NSInteger)index;


- (void)GMGridView:(GMGridView *)gridView changedEdit:(BOOL)edit;

@end @end




Expand Down
88 changes: 63 additions & 25 deletions GMGridView/GMGridView.m
Expand Up @@ -45,7 +45,7 @@ @interface GMGridView () <UIGestureRecognizerDelegate, UIScrollViewDelegate>
{ {
// Sorting Gestures // Sorting Gestures
UIPanGestureRecognizer *_sortingPanGesture; UIPanGestureRecognizer *_sortingPanGesture;
UILongPressGestureRecognizer *_sortingLongPressGesture; UILongPressGestureRecognizer *_longPressGesture;


// Moving gestures // Moving gestures
UIPinchGestureRecognizer *_pinchGesture; UIPinchGestureRecognizer *_pinchGesture;
Expand Down Expand Up @@ -86,7 +86,7 @@ - (void)commonInit;


// Gestures // Gestures
- (void)sortingPanGestureUpdated:(UIPanGestureRecognizer *)panGesture; - (void)sortingPanGestureUpdated:(UIPanGestureRecognizer *)panGesture;
- (void)sortingLongPressGestureUpdated:(UILongPressGestureRecognizer *)longPressGesture; - (void)longPressGestureUpdated:(UILongPressGestureRecognizer *)longPressGesture;
- (void)tapGestureUpdated:(UITapGestureRecognizer *)tapGesture; - (void)tapGestureUpdated:(UITapGestureRecognizer *)tapGesture;
- (void)panGestureUpdated:(UIPanGestureRecognizer *)panGesture; - (void)panGestureUpdated:(UIPanGestureRecognizer *)panGesture;
- (void)pinchGestureUpdated:(UIPinchGestureRecognizer *)pinchGesture; - (void)pinchGestureUpdated:(UIPinchGestureRecognizer *)pinchGesture;
Expand Down Expand Up @@ -144,6 +144,8 @@ @implementation GMGridView
@synthesize minEdgeInsets = _minEdgeInsets; @synthesize minEdgeInsets = _minEdgeInsets;
@synthesize showFullSizeViewWithAlphaWhenTransforming; @synthesize showFullSizeViewWithAlphaWhenTransforming;
@synthesize editing = _editing; @synthesize editing = _editing;
@synthesize enableEditOnLongPress;
@synthesize disableEditOnEmptySpaceTap;


@synthesize itemsSubviewsCacheIsValid = _itemsSubviewsCacheIsValid; @synthesize itemsSubviewsCacheIsValid = _itemsSubviewsCacheIsValid;
@synthesize itemSubviewsCache; @synthesize itemSubviewsCache;
Expand Down Expand Up @@ -185,6 +187,7 @@ - (void)commonInit
_tapGesture.delegate = self; _tapGesture.delegate = self;
_tapGesture.numberOfTapsRequired = 1; _tapGesture.numberOfTapsRequired = 1;
_tapGesture.numberOfTouchesRequired = 1; _tapGesture.numberOfTouchesRequired = 1;
_tapGesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:_tapGesture]; [self addGestureRecognizer:_tapGesture];


///////////////////////////// /////////////////////////////
Expand All @@ -210,10 +213,11 @@ - (void)commonInit
_sortingPanGesture.delegate = self; _sortingPanGesture.delegate = self;
[self addGestureRecognizer:_sortingPanGesture]; [self addGestureRecognizer:_sortingPanGesture];


_sortingLongPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(sortingLongPressGestureUpdated:)]; _longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureUpdated:)];
_sortingLongPressGesture.numberOfTouchesRequired = 1; _longPressGesture.numberOfTouchesRequired = 1;
_sortingLongPressGesture.delegate = self; _longPressGesture.delegate = self;
[self addGestureRecognizer:_sortingLongPressGesture]; _longPressGesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:_longPressGesture];


//////////////////////// ////////////////////////
// Gesture dependencies // Gesture dependencies
Expand Down Expand Up @@ -415,17 +419,21 @@ - (void)setMinEdgeInsets:(UIEdgeInsets)minEdgeInsets


- (void)setMinimumPressDuration:(CFTimeInterval)duration - (void)setMinimumPressDuration:(CFTimeInterval)duration
{ {
_sortingLongPressGesture.minimumPressDuration = duration; _longPressGesture.minimumPressDuration = duration;
} }


- (CFTimeInterval)minimumPressDuration - (CFTimeInterval)minimumPressDuration
{ {
return _sortingLongPressGesture.minimumPressDuration; return _longPressGesture.minimumPressDuration;
} }


- (void)setEditing:(BOOL)editing - (void)setEditing:(BOOL)editing
{ {
[self setEditing:editing animated:NO]; [self setEditing:editing animated:NO];

if ([self.actionDelegate respondsToSelector:@selector(GMGridView:changedEdit:)]) {
[self.actionDelegate GMGridView:self changedEdit:editing];
}
} }


- (void)setEditing:(BOOL)editing animated:(BOOL)animated - (void)setEditing:(BOOL)editing animated:(BOOL)animated
Expand Down Expand Up @@ -481,15 +489,22 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer


if (gestureRecognizer == _tapGesture) if (gestureRecognizer == _tapGesture)
{ {
valid = !isScrolling && !self.isEditing && ![_sortingLongPressGesture hasRecognizedValidGesture]; if (self.editing && self.disableEditOnEmptySpaceTap) {
CGPoint locationTouch = [_tapGesture locationInView:self];
NSInteger position = [self.layoutStrategy itemPositionFromLocation:locationTouch];

valid = (position == GMGV_INVALID_POSITION);
} else {
valid = !isScrolling && !self.isEditing && ![_longPressGesture hasRecognizedValidGesture];
}
} }
else if (gestureRecognizer == _sortingLongPressGesture) else if (gestureRecognizer == _longPressGesture)
{ {
valid = !isScrolling && !self.isEditing && (self.sortingDelegate != nil); valid = (self.sortingDelegate || self.enableEditOnLongPress) && !isScrolling && !self.isEditing;
} }
else if (gestureRecognizer == _sortingPanGesture) else if (gestureRecognizer == _sortingPanGesture)
{ {
valid = (_sortMovingItem != nil && [_sortingLongPressGesture hasRecognizedValidGesture]); valid = (_sortMovingItem != nil && [_longPressGesture hasRecognizedValidGesture]);
} }
else if(gestureRecognizer == _rotationGesture || gestureRecognizer == _pinchGesture || gestureRecognizer == _panGesture) else if(gestureRecognizer == _rotationGesture || gestureRecognizer == _pinchGesture || gestureRecognizer == _panGesture)
{ {
Expand All @@ -516,8 +531,21 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
#pragma mark Sorting gestures & logic #pragma mark Sorting gestures & logic
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////


- (void)sortingLongPressGestureUpdated:(UILongPressGestureRecognizer *)longPressGesture - (void)longPressGestureUpdated:(UILongPressGestureRecognizer *)longPressGesture
{ {
if (self.enableEditOnLongPress && !self.editing) {
CGPoint locationTouch = [longPressGesture locationInView:self];
NSInteger position = [self.layoutStrategy itemPositionFromLocation:locationTouch];

if (position != GMGV_INVALID_POSITION)
{
if (!self.editing) {
self.editing = YES;
}
}
return;
}

switch (longPressGesture.state) switch (longPressGesture.state)
{ {
case UIGestureRecognizerStateBegan: case UIGestureRecognizerStateBegan:
Expand Down Expand Up @@ -1117,11 +1145,21 @@ - (void)tapGestureUpdated:(UITapGestureRecognizer *)tapGesture


if (position != GMGV_INVALID_POSITION) if (position != GMGV_INVALID_POSITION)
{ {
[self.actionDelegate GMGridView:self didTapOnItemAtIndex:position]; if (!self.editing) {
[self cellForItemAtIndex:position].highlighted = NO;
[self.actionDelegate GMGridView:self didTapOnItemAtIndex:position];
}
} }
else if([self.actionDelegate respondsToSelector:@selector(GMGridViewDidTapOnEmptySpace:)]) else
{ {
[self.actionDelegate GMGridViewDidTapOnEmptySpace:self]; if([self.actionDelegate respondsToSelector:@selector(GMGridViewDidTapOnEmptySpace:)])
{
[self.actionDelegate GMGridViewDidTapOnEmptySpace:self];
}

if (self.disableEditOnEmptySpaceTap) {
self.editing = NO;
}
} }
} }


Expand Down Expand Up @@ -1594,14 +1632,14 @@ - (void)insertObjectAtIndex:(NSInteger)index withAnimation:(GMGridViewItemAnimat
oldView.tag = oldView.tag + 1; oldView.tag = oldView.tag + 1;
} }


if (animation & GMGridViewItemAnimationFade) { if (animation & GMGridViewItemAnimationFade) {
cell.alpha = 0; cell.alpha = 0;
[UIView beginAnimations:nil context:NULL]; [UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:kDefaultAnimationDuration]; [UIView setAnimationDelay:kDefaultAnimationDuration];
[UIView setAnimationDuration:kDefaultAnimationDuration]; [UIView setAnimationDuration:kDefaultAnimationDuration];
cell.alpha = 1.0; cell.alpha = 1.0;
[UIView commitAnimations]; [UIView commitAnimations];
} }
[self addSubview:cell]; [self addSubview:cell];
} }


Expand Down
1 change: 1 addition & 0 deletions GMGridView/GMGridViewCell.h
Expand Up @@ -35,6 +35,7 @@
@property (nonatomic, strong) UIImage *deleteButtonIcon; // Delete button image @property (nonatomic, strong) UIImage *deleteButtonIcon; // Delete button image
@property (nonatomic) CGPoint deleteButtonOffset; // Delete button offset relative to the origin @property (nonatomic) CGPoint deleteButtonOffset; // Delete button offset relative to the origin
@property (nonatomic, strong) NSString *reuseIdentifier; @property (nonatomic, strong) NSString *reuseIdentifier;
@property (nonatomic, getter=isHighlighted) BOOL highlighted;


/// Override to release custom data before cell is reused. /// Override to release custom data before cell is reused.
- (void)prepareForReuse; - (void)prepareForReuse;
Expand Down
33 changes: 30 additions & 3 deletions GMGridView/GMGridViewCell.m
Expand Up @@ -57,6 +57,7 @@ @implementation GMGridViewCell
@synthesize deleteButtonIcon = _deleteButtonIcon; @synthesize deleteButtonIcon = _deleteButtonIcon;
@synthesize deleteButtonOffset; @synthesize deleteButtonOffset;
@synthesize reuseIdentifier; @synthesize reuseIdentifier;
@synthesize highlighted;


////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
#pragma mark Constructors #pragma mark Constructors
Expand All @@ -77,7 +78,6 @@ - (id)initWithFrame:(CGRect)frame
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.deleteButton = deleteButton; self.deleteButton = deleteButton;
[self.deleteButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.deleteButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.deleteButton.showsTouchWhenHighlighted = YES;
self.deleteButtonIcon = nil; self.deleteButtonIcon = nil;
self.deleteButtonOffset = CGPointMake(-5, -5); self.deleteButtonOffset = CGPointMake(-5, -5);
self.deleteButton.alpha = 0; self.deleteButton.alpha = 0;
Expand Down Expand Up @@ -106,6 +106,21 @@ - (void)layoutSubviews
} }
} }


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.highlighted = YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.highlighted = NO;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
self.highlighted = NO;
}

////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
#pragma mark Setters / getters #pragma mark Setters / getters
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -183,7 +198,7 @@ - (void)setEditing:(BOOL)editing animated:(BOOL)animated
}else { }else {
self.deleteButton.alpha = editing ? 1.f : 0.f; self.deleteButton.alpha = editing ? 1.f : 0.f;
} }

self.contentView.userInteractionEnabled = !editing; self.contentView.userInteractionEnabled = !editing;
[self shakeStatus:editing]; [self shakeStatus:editing];
} }
Expand Down Expand Up @@ -235,6 +250,18 @@ - (UIImage *)deleteButtonIcon
return [self.deleteButton currentImage]; return [self.deleteButton currentImage];
} }



- (void)setHighlighted:(BOOL)aHighlighted {
highlighted = aHighlighted;

[self.contentView recursiveEnumerateSubviewsUsingBlock:^(UIView *view, BOOL *stop) {
if ([view respondsToSelector:@selector(setHighlighted:)]) {
[(UIControl*)view setHighlighted:highlighted];
}
}];
}


////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
#pragma mark Private methods #pragma mark Private methods
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -292,7 +319,7 @@ - (void)switchToFullSizeMode:(BOOL)fullSizeEnabled
completion:^(BOOL finished){ completion:^(BOOL finished){
[self setNeedsLayout]; [self setNeedsLayout];
} }
]; ];
} }
else else
{ {
Expand Down
2 changes: 1 addition & 1 deletion GMGridView/UIView+GMGridViewAdditions.h
Expand Up @@ -32,5 +32,5 @@
@interface UIView (GMGridViewAdditions) @interface UIView (GMGridViewAdditions)


- (void)shakeStatus:(BOOL)enabled; - (void)shakeStatus:(BOOL)enabled;

- (void)recursiveEnumerateSubviewsUsingBlock:(void (^)(UIView *view, BOOL *stop))block;
@end @end
13 changes: 13 additions & 0 deletions GMGridView/UIView+GMGridViewAdditions.m
Expand Up @@ -61,4 +61,17 @@ - (void)shakeStatus:(BOOL)enabled
} }
} }


- (void)recursiveEnumerateSubviewsUsingBlock:(void (^)(UIView *view, BOOL *stop))block {
if (self.subviews.count == 0) {
return;
}
for (UIView *subview in [self subviews]) {
BOOL stop = NO;
block(subview, &stop);
if (stop) {
return;
}
[subview recursiveEnumerateSubviewsUsingBlock:block];
}
}
@end @end

0 comments on commit 0135d17

Please sign in to comment.