Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Maintain selection during insertion and deletion when possible, and d…

…eselect when not possible. Give the user more control over selection and scrolling.
  • Loading branch information...
commit 94f5074155d3d3f678667b24936edef23b49bba7 1 parent 7333bc6
BJ Homer authored
View
1  Classes/HSSidebarView.h
@@ -21,6 +21,7 @@
@property (assign) NSInteger selectedIndex;
- (BOOL)imageAtIndexIsVisible:(NSUInteger)anIndex;
+- (void)scrollRowAtIndexToVisible:(NSUInteger)anIndex;
- (void)insertRowAtIndex:(NSUInteger)anIndex;
- (void)deleteRowAtIndex:(NSUInteger)anIndex;
View
35 Classes/HSSidebarView.m
@@ -18,6 +18,7 @@ @interface HSSidebarView ()
@property (retain) NSMutableArray *imageViews;
@property (retain) NSMutableArray *viewsForReuse;
@property (retain) NSMutableIndexSet *indexesToAnimate;
+@property (assign) BOOL shouldAnimateSelectionLayer;
@property (assign) BOOL initialized;
@@ -45,6 +46,7 @@ @implementation HSSidebarView
@synthesize imageViews;
@synthesize viewsForReuse;
@synthesize indexesToAnimate;
+@synthesize shouldAnimateSelectionLayer;
@synthesize selectionGradient;
@synthesize initialized;
@synthesize viewBeingDragged;
@@ -199,8 +201,9 @@ - (void)layoutSubviews {
// Draw selection layer
if (selectedIndex >= 0) {
+ CFBooleanRef disableAnimations = shouldAnimateSelectionLayer ? kCFBooleanFalse : kCFBooleanTrue;
[CATransaction begin];
- [CATransaction setValue:(id)kCFBooleanTrue
+ [CATransaction setValue:(id)disableAnimations
forKey:kCATransactionDisableActions];
selectionGradient.hidden = NO;
@@ -208,6 +211,9 @@ - (void)layoutSubviews {
_scrollView.bounds.size.width,
rowHeight);
[CATransaction commit];
+
+ // If we should animate, it will explicitly be reset to YES later.
+ self.shouldAnimateSelectionLayer = NO;
}
else {
selectionGradient.hidden = YES;
@@ -229,13 +235,12 @@ - (void) reloadData {
[self setNeedsLayout];
}
-- (void)insertRowAtIndex:(NSUInteger)anIndex {
- [imageViews insertObject:[NSNull null] atIndex:anIndex];
- [indexesToAnimate addIndex:anIndex];
- self.selectedIndex = anIndex;
-
+- (void)scrollRowAtIndexToVisible:(NSUInteger)anIndex {
+ if (anIndex > self.imageCount - 1) {
+ return;
+ }
CGRect scrollBounds = _scrollView.bounds;
- CGRect imageFrame = [self imageViewFrameInScrollViewForIndex:self.selectedIndex];
+ CGRect imageFrame = [self imageViewFrameInScrollViewForIndex:anIndex];
CGFloat scrollTop = CGRectGetMinY(scrollBounds);
CGFloat scrollBottom = CGRectGetMaxY(scrollBounds);
@@ -270,6 +275,16 @@ - (void)insertRowAtIndex:(NSUInteger)anIndex {
[_scrollView setContentOffset:newOffset animated:YES];
}
+}
+
+- (void)insertRowAtIndex:(NSUInteger)anIndex {
+ [imageViews insertObject:[NSNull null] atIndex:anIndex];
+ [indexesToAnimate addIndex:anIndex];
+
+ if (selectedIndex != -1 && anIndex < selectedIndex) {
+ self.selectedIndex += 1;
+ self.shouldAnimateSelectionLayer = YES;
+ }
[self recalculateScrollViewContentSize];
[self setNeedsLayout];
@@ -280,8 +295,12 @@ - (void)deleteRowAtIndex:(NSUInteger)anIndex {
[self enqueueReusableImageView:selectedView];
[imageViews removeObjectAtIndex:anIndex];
- if (anIndex > selectedIndex || anIndex == self.imageCount) {
+ if (selectedIndex != -1 && anIndex < selectedIndex) {
self.selectedIndex -= 1;
+ self.shouldAnimateSelectionLayer = YES;
+ }
+ else if (selectedIndex == anIndex) {
+ self.selectedIndex = -1;
}
[self recalculateScrollViewContentSize];
View
12 Classes/SidebarViewController.m
@@ -68,13 +68,25 @@ - (IBAction)insertRow:(id)sender {
[colors insertObject:[NSNumber numberWithInt:arc4random()%3]
atIndex:insertionIndex];
[_sidebar insertRowAtIndex:insertionIndex];
+ [_sidebar scrollRowAtIndexToVisible:insertionIndex];
+ _sidebar.selectedIndex = insertionIndex;
}
- (IBAction)deleteSelection:(id)sender {
NSInteger selectedIndex = _sidebar.selectedIndex;
if (selectedIndex != -1) {
+ BOOL isLastRow = (selectedIndex == ([colors count] - 1));
[colors removeObjectAtIndex:selectedIndex];
[_sidebar deleteRowAtIndex:selectedIndex];
+
+ if ([colors count] != 0) {
+ NSUInteger newSelection = selectedIndex;
+ if (isLastRow) {
+ newSelection = [colors count] - 1;
+ }
+ _sidebar.selectedIndex = newSelection;
+ [_sidebar scrollRowAtIndexToVisible:newSelection];
+ }
}
}
View
2  Sidebar.xcodeproj/project.pbxproj
@@ -384,6 +384,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Sidebar-Prefix.pch";
INFOPLIST_FILE = "Sidebar-Info.plist";
+ LINKER_DISPLAYS_MANGLED_NAMES = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
@@ -397,6 +398,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Sidebar-Prefix.pch";
INFOPLIST_FILE = "Sidebar-Info.plist";
+ LINKER_DISPLAYS_MANGLED_NAMES = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
VALIDATE_PRODUCT = YES;
WRAPPER_EXTENSION = app;

0 comments on commit 94f5074

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