Skip to content

Commit

Permalink
Shift other images on the fly while we're dragging.
Browse files Browse the repository at this point in the history
  • Loading branch information
BJ Homer authored and BJ Homer committed Nov 21, 2010
1 parent 5942415 commit 4a7d644
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
53 changes: 41 additions & 12 deletions Classes/HSSidebarView.m
Expand Up @@ -17,7 +17,10 @@ @interface HSSidebarView ()

@property (retain) NSMutableArray *imageViews;
@property (assign) BOOL initialized;

@property (retain) UIView *viewBeingDragged;
@property (assign) NSInteger draggedViewOldIndex;
@property (assign) CGFloat dragOffsetY;

// readwrite overrides of public readonly methods
@property (readwrite) NSUInteger imageCount;
Expand All @@ -36,6 +39,8 @@ @implementation HSSidebarView
@synthesize selectionGradient;
@synthesize initialized;
@synthesize viewBeingDragged;
@synthesize draggedViewOldIndex;
@synthesize dragOffsetY;
@synthesize selectedIndex;
@synthesize imageCount;
@synthesize delegate;
Expand Down Expand Up @@ -109,13 +114,17 @@ - (void)layoutSubviews {
self.initialized = YES;
}
else {
[UIView animateWithDuration:0.1
animations:
^{
[imageViews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
view.center = [self imageViewCenterInScrollViewForIndex:idx];
}];
}];
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[imageViews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
if (view != self.viewBeingDragged) {
view.center = [self imageViewCenterInScrollViewForIndex:idx];
}
}];
}
completion:NULL];
}

// Draw selection layer
Expand Down Expand Up @@ -172,25 +181,45 @@ - (void)pressedSidebar:(UILongPressGestureRecognizer *)recognizer {

if (recognizer.state == UIGestureRecognizerStateBegan) {
self.selectedIndex = -1;
hitView.alpha = 0.5;
[UIView animateWithDuration:0.1
animations:^{
hitView.alpha = 0.5;
hitView.transform = CGAffineTransformMakeScale(1.1, 1.1);
}
];
self.viewBeingDragged = hitView;
self.draggedViewOldIndex = currentIndex;
self.dragOffsetY = hitY - [self imageViewCenterInScrollViewForIndex:currentIndex].y;
[_scrollView bringSubviewToFront:viewBeingDragged];
}
else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint newPosition = [recognizer locationInView:_scrollView];
viewBeingDragged.center = CGPointMake(viewBeingDragged.center.x, newPosition.y);
viewBeingDragged.center = CGPointMake(viewBeingDragged.center.x, newPosition.y - self.dragOffsetY);
[imageViews removeObject:viewBeingDragged];
[imageViews insertObject:viewBeingDragged atIndex:currentIndex];
[self setNeedsLayout];
}
else {
CGPoint finalPosition = [self imageViewCenterInScrollViewForIndex:currentIndex];
[UIView animateWithDuration:0.1
[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];
self.selectedIndex = currentIndex;
[self setNeedsLayout];

if ([delegate respondsToSelector:@selector(sidebar:didMoveImageAtIndex:toIndex:)]) {
[delegate sidebar:self didMoveImageAtIndex:self.draggedViewOldIndex toIndex:currentIndex];
}

self.draggedViewOldIndex = -1;
self.dragOffsetY = 0;
self.viewBeingDragged = nil;
}
}
Expand Down
1 change: 1 addition & 0 deletions Classes/HSSidebarViewDelegate.h
Expand Up @@ -15,6 +15,7 @@
#pragma mark Delegate methods
@optional
- (void)sidebar:(HSSidebarView *)sidebar didTapImageAtIndex:(NSUInteger)anIndex;
- (void)sidebar:(HSSidebarView *)sidebar didMoveImageAtIndex:(NSUInteger)oldIndex toIndex:(NSUInteger)newIndex;

#pragma mark -
#pragma mark Data source methods
Expand Down
4 changes: 4 additions & 0 deletions Classes/SidebarViewController.m
Expand Up @@ -69,6 +69,10 @@ -(void)sidebar:(HSSidebarView *)sidebar didTapImageAtIndex:(NSUInteger)anIndex {
NSLog(@"Touched selected image at index: %u", anIndex);
}

- (void)sidebar:(HSSidebarView *)sidebar didMoveImageAtIndex:(NSUInteger)oldIndex toIndex:(NSUInteger)newIndex {
NSLog(@"Image at index %d moved to index %d", oldIndex, newIndex);
}

- (void)dealloc {
[_sidebar release];
[super dealloc];
Expand Down

0 comments on commit 4a7d644

Please sign in to comment.