Skip to content
This repository

Sorting Support #160

Open
wants to merge 5 commits into from

1 participant

Chad Podoski
Chad Podoski

Figured I would issue this pull request since the feature is still being requested in the issue tracker on the repository. It adds drag/drop sorting, fixes a bug related to positioning the dragged item based on original grid index, and addresses some edge aliasing that was occurring for me during reorder. I've moved to mainly using collection views only, so I will probably delete my fork after this pull request is either merged or it becomes apparent that it won't be merged. Thanks for the great grid view!

chadpod added some commits
Chad Podoski chadpod Manually merger pull request #47, "sortingDelegate additions: allow/d…
…isallow movement and insertion of items at certain index"
96581cd
Chad Podoski chadpod Rasterize cells prior to shake animation to get rid of edge aliasing.
Change from using a transform to reposition dragged cell to using frame positioning based on gesture offset and original cell position.
Enabled sorting while in edit mode.
86c6436
Chad Podoski chadpod Fix bug related to sorting while scrolling through grid. Had to store…
… original cell rect, instead of original index.
1244951
Chad Podoski chadpod Needed to set rasterizationScale for cell shake animation, so it had …
…proper resolution on Retina displays.
f7b5d42
Chad Podoski chadpod Tweak the animation characteristics of the icon wiggle. 6aca766
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 5 unique commits by 1 author.

Aug 02, 2012
Chad Podoski chadpod Manually merger pull request #47, "sortingDelegate additions: allow/d…
…isallow movement and insertion of items at certain index"
96581cd
Aug 07, 2012
Chad Podoski chadpod Rasterize cells prior to shake animation to get rid of edge aliasing.
Change from using a transform to reposition dragged cell to using frame positioning based on gesture offset and original cell position.
Enabled sorting while in edit mode.
86c6436
Chad Podoski chadpod Fix bug related to sorting while scrolling through grid. Had to store…
… original cell rect, instead of original index.
1244951
Aug 14, 2012
Chad Podoski chadpod Needed to set rasterizationScale for cell shake animation, so it had …
…proper resolution on Retina displays.
f7b5d42
Aug 18, 2012
Chad Podoski chadpod Tweak the animation characteristics of the icon wiggle. 6aca766
This page is out of date. Refresh to see the latest.
5 GMGridView/GMGridView.h
@@ -172,6 +172,11 @@ typedef enum
172 172 // Enable/Disable the shaking behavior of an item being moved
173 173 - (BOOL)GMGridView:(GMGridView *)gridView shouldAllowShakingBehaviorWhenMovingCell:(GMGridViewCell *)view atIndex:(NSInteger)index;
174 174
  175 +// Allow/Disallow moving of an item
  176 +- (BOOL)GMGridView:(GMGridView *)gridView shouldAllowMovingCell:(GMGridViewCell *)view atIndex:(NSInteger)index;
  177 +// Allow/Disallow placing item at index
  178 +- (BOOL)GMGridView:(GMGridView *)gridView shouldAllowMovingCell:(GMGridViewCell *)view toIndex:(NSInteger)index;
  179 +
175 180 @end
176 181
177 182 //////////////////////////////////////////////////////////////
46 GMGridView/GMGridView.m
@@ -31,6 +31,7 @@
31 31 #import "GMGridViewCell+Extended.h"
32 32 #import "GMGridViewLayoutStrategies.h"
33 33 #import "UIGestureRecognizer+GMGridViewAdditions.h"
  34 +#import "UIView+GMGridViewAdditions.h"
34 35
35 36 static const NSInteger kTagOffset = 50;
36 37 static const CGFloat kDefaultAnimationDuration = 0.3;
@@ -60,6 +61,7 @@ @interface GMGridView () <UIGestureRecognizerDelegate, UIScrollViewDelegate>
60 61
61 62 // Moving (sorting) control vars
62 63 GMGridViewCell *_sortMovingItem;
  64 + CGRect _sortOriginalFrame;
63 65 NSInteger _sortFuturePosition;
64 66 BOOL _autoScrollActive;
65 67
@@ -250,6 +252,7 @@ - (void)commonInit
250 252 self.minEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
251 253 self.clipsToBounds = NO;
252 254
  255 + _sortOriginalFrame = CGRectZero;
253 256 _sortFuturePosition = GMGV_INVALID_POSITION;
254 257 _itemSize = CGSizeZero;
255 258 _centerGrid = YES;
@@ -500,7 +503,7 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
500 503 }
501 504 else if (gestureRecognizer == _longPressGesture)
502 505 {
503   - valid = (self.sortingDelegate || self.enableEditOnLongPress) && !isScrolling && !self.isEditing;
  506 + valid = (self.sortingDelegate || self.enableEditOnLongPress) && !isScrolling;
504 507 }
505 508 else if (gestureRecognizer == _sortingPanGesture)
506 509 {
@@ -533,19 +536,18 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
533 536
534 537 - (void)longPressGestureUpdated:(UILongPressGestureRecognizer *)longPressGesture
535 538 {
536   - if (self.enableEditOnLongPress && !self.editing) {
  539 + if (self.enableEditOnLongPress && !self.editing) {
537 540 CGPoint locationTouch = [longPressGesture locationInView:self];
538 541 NSInteger position = [self.layoutStrategy itemPositionFromLocation:locationTouch];
539 542
540   - if (position != GMGV_INVALID_POSITION)
  543 + if (position != GMGV_INVALID_POSITION)
541 544 {
542 545 if (!self.editing) {
543 546 self.editing = YES;
544 547 }
545 548 }
546   - return;
547 549 }
548   -
  550 +
549 551 switch (longPressGesture.state)
550 552 {
551 553 case UIGestureRecognizerStateBegan:
@@ -556,6 +558,14 @@ - (void)longPressGestureUpdated:(UILongPressGestureRecognizer *)longPressGesture
556 558
557 559 NSInteger position = [self.layoutStrategy itemPositionFromLocation:location];
558 560
  561 + // Ask the delegate if moving is permitted
  562 + if ([self.sortingDelegate respondsToSelector:@selector(GMGridView:shouldAllowMovingCell:atIndex:)])
  563 + {
  564 + GMGridViewCell *item = [self cellForItemAtIndex:position];
  565 + if (![self.sortingDelegate GMGridView:self shouldAllowMovingCell:item atIndex:position])
  566 + position = GMGV_INVALID_POSITION;
  567 + }
  568 +
559 569 if (position != GMGV_INVALID_POSITION)
560 570 {
561 571 [self sortingMoveDidStartAtPoint:location];
@@ -607,7 +617,8 @@ - (void)sortingPanGestureUpdated:(UIPanGestureRecognizer *)panGesture
607 617 CGPoint offset = translation;
608 618 CGPoint locationInScroll = [panGesture locationInView:self];
609 619
610   - _sortMovingItem.transform = CGAffineTransformMakeTranslation(offset.x, offset.y);
  620 + _sortMovingItem.frame = CGRectMake(_sortOriginalFrame.origin.x + offset.x, _sortOriginalFrame.origin.y + offset.y, _sortOriginalFrame.size.width, _sortOriginalFrame.size.height);
  621 +
611 622 [self sortingMoveDidContinueToPoint:locationInScroll];
612 623
613 624 break;
@@ -623,8 +634,7 @@ - (void)sortingAutoScrollMovementCheck
623 634 {
624 635 CGPoint locationInMainView = [_sortingPanGesture locationInView:self];
625 636 locationInMainView = CGPointMake(locationInMainView.x - self.contentOffset.x,
626   - locationInMainView.y -self.contentOffset.y
627   - );
  637 + locationInMainView.y - self.contentOffset.y);
628 638
629 639
630 640 CGFloat threshhold = _itemSize.height;
@@ -706,6 +716,7 @@ - (void)sortingMoveDidStartAtPoint:(CGPoint)point
706 716 NSInteger position = [self.layoutStrategy itemPositionFromLocation:point];
707 717
708 718 GMGridViewCell *item = [self cellForItemAtIndex:position];
  719 + [item shakeStatus:NO];
709 720
710 721 [self bringSubviewToFront:item];
711 722 _sortMovingItem = item;
@@ -714,7 +725,13 @@ - (void)sortingMoveDidStartAtPoint:(CGPoint)point
714 725
715 726 [_sortMovingItem removeFromSuperview];
716 727 _sortMovingItem.frame = frameInMainView;
  728 + _sortOriginalFrame = frameInMainView;
717 729 [self.mainSuperView addSubview:_sortMovingItem];
  730 +
  731 + [UIView animateWithDuration:0.2f
  732 + animations:^{
  733 + _sortMovingItem.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
  734 + }];
718 735
719 736 _sortFuturePosition = _sortMovingItem.tag - kTagOffset;
720 737 _sortMovingItem.tag = 0;
@@ -737,6 +754,10 @@ - (void)sortingMoveDidStartAtPoint:(CGPoint)point
737 754 - (void)sortingMoveDidStopAtPoint:(CGPoint)point
738 755 {
739 756 [_sortMovingItem shake:NO];
  757 + [UIView animateWithDuration:0.2f
  758 + animations:^{
  759 + _sortMovingItem.transform = CGAffineTransformIdentity;
  760 + }];
740 761
741 762 _sortMovingItem.tag = _sortFuturePosition + kTagOffset;
742 763
@@ -763,6 +784,7 @@ - (void)sortingMoveDidStopAtPoint:(CGPoint)point
763 784 }
764 785
765 786 _sortMovingItem = nil;
  787 + _sortOriginalFrame = CGRectZero;
766 788 _sortFuturePosition = GMGV_INVALID_POSITION;
767 789
768 790 [self setSubviewsCacheAsInvalid];
@@ -775,6 +797,14 @@ - (void)sortingMoveDidContinueToPoint:(CGPoint)point
775 797 int position = [self.layoutStrategy itemPositionFromLocation:point];
776 798 int tag = position + kTagOffset;
777 799
  800 + // Ask the delegate if inserting item is permitted
  801 + if ([self.sortingDelegate respondsToSelector:@selector(GMGridView:shouldAllowMovingCell:toIndex:)])
  802 + {
  803 + GMGridViewCell *item = [self cellForItemAtIndex:position];
  804 + if (![self.sortingDelegate GMGridView:self shouldAllowMovingCell:item toIndex:position])
  805 + position = GMGV_INVALID_POSITION;
  806 + }
  807 +
778 808 if (position != GMGV_INVALID_POSITION && position != _sortFuturePosition && position < _numberTotalItems)
779 809 {
780 810 BOOL positionTaken = NO;
7 GMGridView/UIView+GMGridViewAdditions.m
@@ -43,21 +43,24 @@ - (void)shakeStatus:(BOOL)enabled
43 43 {
44 44 if (enabled)
45 45 {
46   - CGFloat rotation = 0.03;
  46 + CGFloat rotation = 0.02;
47 47
48 48 CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform"];
49   - shake.duration = 0.13;
  49 + shake.duration = 0.09;
50 50 shake.autoreverses = YES;
51 51 shake.repeatCount = MAXFLOAT;
52 52 shake.removedOnCompletion = NO;
53 53 shake.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,-rotation, 0.0 ,0.0 ,1.0)];
54 54 shake.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform, rotation, 0.0 ,0.0 ,1.0)];
55 55
  56 + self.layer.shouldRasterize = YES;
  57 + self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
56 58 [self.layer addAnimation:shake forKey:@"shakeAnimation"];
57 59 }
58 60 else
59 61 {
60 62 [self.layer removeAnimationForKey:@"shakeAnimation"];
  63 + self.layer.shouldRasterize = NO;
61 64 }
62 65 }
63 66

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.