Skip to content
Browse files

Add drop-off deletion

  • Loading branch information...
1 parent b8d4512 commit c219a9acac2d27baf074b2cd8fa38ce76a24ec9b BJ Homer committed
Showing with 96 additions and 52 deletions.
  1. +90 −52 Classes/HSSidebarView.m
  2. +1 −0 Classes/HSSidebarViewDelegate.h
  3. +5 −0 Classes/SidebarViewController.m
View
142 Classes/HSSidebarView.m
@@ -26,7 +26,7 @@ @interface HSSidebarView ()
@property (retain) UIView *viewBeingDragged;
@property (assign) NSInteger draggedViewOldIndex;
-@property (assign) CGFloat dragOffsetY;
+@property (assign) CGPoint dragOffset;
- (void)setupViewHierarchy;
- (void)setupInstanceVariables;
@@ -51,7 +51,7 @@ @implementation HSSidebarView
@synthesize initialized;
@synthesize viewBeingDragged;
@synthesize draggedViewOldIndex;
-@synthesize dragOffsetY;
+@synthesize dragOffset;
@synthesize selectedIndex;
@synthesize dragScrollTimer;
@synthesize delegate;
@@ -97,6 +97,7 @@ - (void) setupViewHierarchy {
_scrollView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
_scrollView.alwaysBounceVertical = YES;
+ _scrollView.clipsToBounds = NO;
[self addSubview:_scrollView];
self.selectionGradient = [CAGradientLayer layer];
@@ -142,7 +143,7 @@ - (void)layoutSubviews {
// Remove any off-screen views
NSMutableIndexSet *indexesToRelease = [NSMutableIndexSet indexSet];
[imageViews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- if (obj != noView && [visibleIndices containsIndex:idx] == NO) {
+ if (obj != noView && obj != viewBeingDragged && [visibleIndices containsIndex:idx] == NO) {
[indexesToRelease addIndex:idx];
[self enqueueReusableImageView:obj];
@@ -334,19 +335,23 @@ - (void)tappedSidebar:(UITapGestureRecognizer *)recognizer {
}
- (void)pressedSidebar:(UILongPressGestureRecognizer *)recognizer {
- CGFloat hitY = [recognizer locationInView:_scrollView].y;
- NSInteger currentIndex = hitY / rowHeight;
+ CGPoint hitPoint = [recognizer locationInView:_scrollView];
+ BOOL isInScrollView = CGRectContainsPoint([_scrollView bounds], hitPoint);
+ NSInteger newIndex = hitPoint.y / rowHeight;
- if (currentIndex > self.imageCount - 1) {
- currentIndex = self.imageCount - 1;
+ if (newIndex > self.imageCount - 1) {
+ newIndex = self.imageCount - 1;
}
- else if (currentIndex < 0) {
- currentIndex = 0;
+ else if (newIndex < 0) {
+ newIndex = 0;
}
- UIImageView *hitView = [self.imageViews objectAtIndex:currentIndex];
+ if (isInScrollView == NO) {
+ newIndex = self.draggedViewOldIndex;
+ }
if (recognizer.state == UIGestureRecognizerStateBegan) {
+ UIImageView *hitView = [self.imageViews objectAtIndex:newIndex];
self.selectedIndex = -1;
[UIView animateWithDuration:0.1
animations:^{
@@ -355,38 +360,48 @@ - (void)pressedSidebar:(UILongPressGestureRecognizer *)recognizer {
}
];
self.viewBeingDragged = hitView;
- self.draggedViewOldIndex = currentIndex;
- self.dragOffsetY = hitY - [self imageViewCenterInScrollViewForIndex:currentIndex].y;
+ self.draggedViewOldIndex = newIndex;
+ self.dragOffset = CGPointMake(hitPoint.x - hitView.center.x, hitPoint.y - hitView.center.y);
[_scrollView bringSubviewToFront:viewBeingDragged];
}
else if (recognizer.state == UIGestureRecognizerStateChanged) {
- CGPoint newPosition = [recognizer locationInView:_scrollView];
- viewBeingDragged.center = CGPointMake(viewBeingDragged.center.x, newPosition.y - self.dragOffsetY);
- [imageViews removeObject:viewBeingDragged];
- [imageViews insertObject:viewBeingDragged atIndex:currentIndex];
- [self setNeedsLayout];
+ viewBeingDragged.center = CGPointMake(hitPoint.x - self.dragOffset.x, hitPoint.y - self.dragOffset.y);
+ if (isInScrollView == NO) {
+ // Don't scroll if we're not over the scrollview
+ [dragScrollTimer invalidate];
+ self.dragScrollTimer = nil;
+
+ [imageViews removeObject:viewBeingDragged];
+ [self setNeedsLayout];
+ }
+ else {
+ [imageViews removeObject:viewBeingDragged];
+ [imageViews insertObject:viewBeingDragged atIndex:newIndex];
+ [self setNeedsLayout];
+
- if (CGRectGetMaxY(_scrollView.bounds) - newPosition.y < 50) {
- if (dragScrollTimer == nil) {
- self.dragScrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
- target:self
- selector:@selector(scrollDown:)
- userInfo:nil
- repeats:YES];
+ if (CGRectGetMaxY(_scrollView.bounds) - hitPoint.y < 50) {
+ if (dragScrollTimer == nil) {
+ self.dragScrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
+ target:self
+ selector:@selector(scrollDown:)
+ userInfo:nil
+ repeats:YES];
+ }
}
- }
- else if (newPosition.y - CGRectGetMinY(_scrollView.bounds) < 50) {
- if (dragScrollTimer == nil) {
- self.dragScrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
- target:self
- selector:@selector(scrollUp:)
- userInfo:nil
- repeats:YES];
+ else if (hitPoint.y - CGRectGetMinY(_scrollView.bounds) < 50) {
+ if (dragScrollTimer == nil) {
+ self.dragScrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
+ target:self
+ selector:@selector(scrollUp:)
+ userInfo:nil
+ repeats:YES];
+ }
+ }
+ else {
+ [dragScrollTimer invalidate];
+ self.dragScrollTimer = nil;
}
- }
- else {
- [dragScrollTimer invalidate];
- self.dragScrollTimer = nil;
}
}
else {
@@ -394,26 +409,49 @@ - (void)pressedSidebar:(UILongPressGestureRecognizer *)recognizer {
[self.dragScrollTimer invalidate];
self.dragScrollTimer = nil;
- CGPoint finalPosition = [self imageViewCenterInScrollViewForIndex:currentIndex];
- [UIView animateWithDuration:0.2
- animations:^{
- viewBeingDragged.center = finalPosition;
- viewBeingDragged.alpha = 1.0;
- viewBeingDragged.transform = CGAffineTransformIdentity;
- }
- completion:^(BOOL finished){
- self.selectedIndex = currentIndex;
- [self setNeedsLayout];
- }];
- [imageViews removeObject:viewBeingDragged];
- [imageViews insertObject:viewBeingDragged atIndex:currentIndex];
+ CGPoint pointInWindow = [self convertPoint:hitPoint toView:nil];
+ CGRect deleteZone = CGRectInset(self.window.bounds, 40, 40);
- if ([delegate respondsToSelector:@selector(sidebar:didMoveImageAtIndex:toIndex:)]) {
- [delegate sidebar:self didMoveImageAtIndex:self.draggedViewOldIndex toIndex:currentIndex];
+ if (isInScrollView || CGRectContainsPoint(deleteZone, pointInWindow) == NO) {
+ CGPoint finalPosition = [self imageViewCenterInScrollViewForIndex:newIndex];
+ [UIView animateWithDuration:0.2
+ animations:^{
+ viewBeingDragged.center = finalPosition;
+ viewBeingDragged.alpha = 1.0;
+ viewBeingDragged.transform = CGAffineTransformIdentity;
+ }
+ completion:^(BOOL finished){
+ self.selectedIndex = newIndex;
+ [self setNeedsLayout];
+ }];
+ [imageViews removeObject:viewBeingDragged];
+ [imageViews insertObject:viewBeingDragged atIndex:newIndex];
+
+ if (draggedViewOldIndex != newIndex && [delegate respondsToSelector:@selector(sidebar:didMoveImageAtIndex:toIndex:)]) {
+ [delegate sidebar:self didMoveImageAtIndex:draggedViewOldIndex toIndex:newIndex];
+ }
+ }
+ else {
+ [UIView animateWithDuration:0.2
+ delay:0
+ options:UIViewAnimationCurveEaseOut
+ animations:^{
+ viewBeingDragged.transform = CGAffineTransformMakeScale(4.0, 4.0);
+ viewBeingDragged.alpha = 0.0;
+ }
+ completion:^(BOOL finished) {
+ self.selectedIndex = -1;
+ [self setNeedsLayout];
+ }];
+ [imageViews removeObject:viewBeingDragged];
+
+ if ([delegate respondsToSelector:@selector(sidebar:didRemoveImageAtIndex:)]) {
+ [delegate sidebar:self didRemoveImageAtIndex:self.draggedViewOldIndex];
+ }
}
self.draggedViewOldIndex = -1;
- self.dragOffsetY = 0;
+ self.dragOffset = CGPointZero;
self.viewBeingDragged = nil;
}
}
View
1 Classes/HSSidebarViewDelegate.h
@@ -16,6 +16,7 @@
@optional
- (void)sidebar:(HSSidebarView *)sidebar didTapImageAtIndex:(NSUInteger)anIndex;
- (void)sidebar:(HSSidebarView *)sidebar didMoveImageAtIndex:(NSUInteger)oldIndex toIndex:(NSUInteger)newIndex;
+- (void)sidebar:(HSSidebarView *)sidebar didRemoveImageAtIndex:(NSUInteger)anIndex;
#pragma mark -
#pragma mark Data source methods
View
5 Classes/SidebarViewController.m
@@ -145,6 +145,11 @@ - (void)sidebar:(HSSidebarView *)sidebar didMoveImageAtIndex:(NSUInteger)oldInde
[color release];
}
+- (void)sidebar:(HSSidebarView *)sidebar didRemoveImageAtIndex:(NSUInteger)anIndex {
+ NSLog(@"Image at index %d removed", anIndex);
+ [colors removeObjectAtIndex:anIndex];
+}
+
- (void)dealloc {
[popover release];
[_sidebar release];

0 comments on commit c219a9a

Please sign in to comment.
Something went wrong with that request. Please try again.