Permalink
Browse files

Support horizontal-ness as well.

  • Loading branch information...
1 parent aa1c385 commit 522f159e68989d27aaf2d5042351625517f74e93 @bjhomer committed Dec 8, 2010
Showing with 145 additions and 55 deletions.
  1. +142 −52 Classes/HSSidebarView.m
  2. +1 −1 Classes/SidebarViewController.m
  3. +1 −1 Sidebar.xcodeproj/project.pbxproj
  4. +1 −1 en.lproj/SidebarViewController.xib
View
@@ -21,6 +21,7 @@ @interface HSSidebarView ()
@property (assign) BOOL shouldAnimateSelectionLayer;
@property (assign) BOOL initialized;
+@property (assign) BOOL isHorizontal;
@property (retain) NSTimer *dragScrollTimer;
@@ -56,21 +57,22 @@ @implementation HSSidebarView
@synthesize dragScrollTimer;
@synthesize delegate;
@synthesize rowHeight;
+@synthesize isHorizontal;
#pragma mark -
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
- [self setupViewHierarchy];
[self setupInstanceVariables];
+ [self setupViewHierarchy];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
- [self setupViewHierarchy];
[self setupInstanceVariables];
+ [self setupViewHierarchy];
}
return self;
}
@@ -92,11 +94,24 @@ - (void)dealloc {
#pragma mark Setup
- (void) setupViewHierarchy {
+
self.scrollView = [[[UIScrollView alloc] initWithFrame:self.bounds] autorelease];
- [_scrollView setAutoresizingMask: UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight];
+ if (self.bounds.size.width > self.bounds.size.height) {
+ isHorizontal = YES;
+ [_scrollView setAutoresizingMask: UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth];
+ }
+ else {
+ [_scrollView setAutoresizingMask: UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight];
+ }
+
_scrollView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
- _scrollView.alwaysBounceVertical = YES;
+ if (isHorizontal == NO) {
+ _scrollView.alwaysBounceVertical = YES;
+ }
+ else {
+ _scrollView.alwaysBounceHorizontal = YES;
+ }
_scrollView.clipsToBounds = NO;
[self addSubview:_scrollView];
@@ -106,7 +121,12 @@ - (void) setupViewHierarchy {
UIColor *topColor = [baseColor colorWithAlphaComponent:1];
UIColor *bottomColor = [baseColor colorWithAlphaComponent:0.75];
selectionGradient.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
- selectionGradient.bounds = CGRectMake(0, 0, _scrollView.bounds.size.width, rowHeight);
+ if (isHorizontal) {
+ selectionGradient.bounds = CGRectMake(0, 0, rowHeight, _scrollView.bounds.size.height);
+ }
+ else {
+ selectionGradient.bounds = CGRectMake(0, 0, _scrollView.bounds.size.width, rowHeight);
+ }
selectionGradient.hidden = YES;
[_scrollView.layer addSublayer:selectionGradient];
@@ -209,9 +229,7 @@ - (void)layoutSubviews {
forKey:kCATransactionDisableActions];
selectionGradient.hidden = NO;
- selectionGradient.frame = CGRectMake(0, rowHeight * selectedIndex,
- _scrollView.bounds.size.width,
- rowHeight);
+ selectionGradient.position = [self imageViewCenterInScrollViewForIndex:selectedIndex];
[CATransaction commit];
// If we should animate, it will explicitly be reset to YES later.
@@ -223,7 +241,12 @@ - (void)layoutSubviews {
}
- (void)recalculateScrollViewContentSize {
- _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width, self.imageCount*rowHeight);
+ if (isHorizontal) {
+ _scrollView.contentSize = CGSizeMake(self.imageCount*rowHeight, _scrollView.bounds.size.height);
+ }
+ else {
+ _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width, self.imageCount*rowHeight);
+ }
}
- (void) reloadData {
@@ -244,38 +267,75 @@ - (void)scrollRowAtIndexToVisible:(NSUInteger)anIndex {
CGRect scrollBounds = _scrollView.bounds;
CGRect imageFrame = [self imageViewFrameInScrollViewForIndex:anIndex];
- CGFloat scrollTop = CGRectGetMinY(scrollBounds);
- CGFloat scrollBottom = CGRectGetMaxY(scrollBounds);
- CGFloat imageTop = CGRectGetMinY(imageFrame);
- CGFloat imageBottom = CGRectGetMaxY(imageFrame);
-
- CGPoint oldOffset = _scrollView.contentOffset;
-
- if (imageTop < scrollTop) {
- // It's off the top of the screen
- CGFloat distanceBetweenFrameAndRowTop = (int)imageTop % (int)rowHeight;
- CGFloat delta = scrollTop - imageTop + distanceBetweenFrameAndRowTop;
+ if (isHorizontal == NO) {
+ CGFloat scrollTop = CGRectGetMinY(scrollBounds);
+ CGFloat scrollBottom = CGRectGetMaxY(scrollBounds);
+ CGFloat imageTop = CGRectGetMinY(imageFrame);
+ CGFloat imageBottom = CGRectGetMaxY(imageFrame);
- if (anIndex != 0) {
- // Show a bit of the previous row, if one exists
- delta += (rowHeight / 2);
- }
- CGPoint newOffset = CGPointMake(oldOffset.x, oldOffset.y - delta);
+ CGPoint oldOffset = _scrollView.contentOffset;
- [_scrollView setContentOffset:newOffset animated:YES];
+ if (imageTop < scrollTop) {
+ // It's off the top of the screen
+ CGFloat distanceBetweenFrameAndRowTop = (int)imageTop % (int)rowHeight;
+ CGFloat delta = scrollTop - imageTop + distanceBetweenFrameAndRowTop;
+
+ if (anIndex != 0) {
+ // Show a bit of the previous row, if one exists
+ delta += (rowHeight / 2);
+ }
+ CGPoint newOffset = CGPointMake(oldOffset.x, oldOffset.y - delta);
+
+ [_scrollView setContentOffset:newOffset animated:YES];
+ }
+ else if (scrollBottom < imageBottom) {
+ // It's off the bottom of the screen
+ CGFloat distanceBetweenFrameAndRowBottom = rowHeight - ((int)imageBottom % (int)rowHeight);
+
+ CGFloat delta = imageBottom - scrollBottom + distanceBetweenFrameAndRowBottom;
+ if (anIndex != [self imageCount] - 1) {
+ // Show a bit of the next row, if one exists.
+ delta += (rowHeight / 2);
+ }
+ CGPoint newOffset = CGPointMake(oldOffset.x, oldOffset.y + delta);
+
+ [_scrollView setContentOffset:newOffset animated:YES];
+ }
}
- else if (scrollBottom < imageBottom) {
- // It's off the bottom of the screen
- CGFloat distanceBetweenFrameAndRowBottom = rowHeight - ((int)imageBottom % (int)rowHeight);
+ else {
+ CGFloat scrollLeft = CGRectGetMinX(scrollBounds);
+ CGFloat scrollRight = CGRectGetMaxX(scrollBounds);
+ CGFloat imageLeft = CGRectGetMinX(imageFrame);
+ CGFloat imageRight = CGRectGetMaxX(imageFrame);
- CGFloat delta = imageBottom - scrollBottom + distanceBetweenFrameAndRowBottom;
- if (anIndex != [self imageCount] - 1) {
- // Show a bit of the next row, if one exists.
- delta += (rowHeight / 2);
- }
- CGPoint newOffset = CGPointMake(oldOffset.x, oldOffset.y + delta);
+ CGPoint oldOffset = _scrollView.contentOffset;
- [_scrollView setContentOffset:newOffset animated:YES];
+ if (imageLeft < scrollLeft) {
+ // It's off the top of the screen
+ CGFloat distanceBetweenFrameAndRowLeft = (int)imageLeft % (int)rowHeight;
+ CGFloat delta = scrollLeft - imageLeft + distanceBetweenFrameAndRowLeft;
+
+ if (anIndex != 0) {
+ // Show a bit of the previous row, if one exists
+ delta += (rowHeight / 2);
+ }
+ CGPoint newOffset = CGPointMake(oldOffset.x - delta, oldOffset.y);
+
+ [_scrollView setContentOffset:newOffset animated:YES];
+ }
+ else if (scrollRight < imageRight) {
+ // It's off the bottom of the screen
+ CGFloat distanceBetweenFrameAndRowRight = rowHeight - ((int)imageRight % (int)rowHeight);
+
+ CGFloat delta = imageRight - scrollRight + distanceBetweenFrameAndRowRight;
+ if (anIndex != [self imageCount] - 1) {
+ // Show a bit of the next row, if one exists.
+ delta += (rowHeight / 2);
+ }
+ CGPoint newOffset = CGPointMake(oldOffset.x + delta, oldOffset.y);
+
+ [_scrollView setContentOffset:newOffset animated:YES];
+ }
}
}
@@ -294,7 +354,9 @@ - (void)insertRowAtIndex:(NSUInteger)anIndex {
- (void)deleteRowAtIndex:(NSUInteger)anIndex {
UIImageView *selectedView = [imageViews objectAtIndex:anIndex];
- [self enqueueReusableImageView:selectedView];
+ if ([selectedView isKindOfClass:[NSNull class]] == NO) {
+ [self enqueueReusableImageView:selectedView];
+ }
[imageViews removeObjectAtIndex:anIndex];
if (selectedIndex != -1 && anIndex < selectedIndex) {
@@ -313,8 +375,8 @@ - (void)deleteRowAtIndex:(NSUInteger)anIndex {
- (void)tappedSidebar:(UITapGestureRecognizer *)recognizer {
UIView *hitView = [self hitTest:[recognizer locationInView:self] withEvent:nil];
if (hitView == _scrollView) {
- CGFloat hitY = [recognizer locationInView:_scrollView].y;
- NSInteger newSelection = hitY / rowHeight;
+ CGPoint hitPoint = [recognizer locationInView:_scrollView];
+ NSInteger newSelection = ((isHorizontal ? hitPoint.x : hitPoint.y) / rowHeight);
if (newSelection > self.imageCount - 1) {
self.selectedIndex = -1;
@@ -338,7 +400,7 @@ - (void)tappedSidebar:(UITapGestureRecognizer *)recognizer {
- (void)pressedSidebar:(UILongPressGestureRecognizer *)recognizer {
CGPoint hitPoint = [recognizer locationInView:_scrollView];
BOOL isInScrollView = CGRectContainsPoint([_scrollView bounds], hitPoint);
- NSInteger newIndex = hitPoint.y / rowHeight;
+ NSInteger newIndex = (isHorizontal ? hitPoint.x : hitPoint.y) / rowHeight;
if (newIndex > self.imageCount - 1) {
newIndex = self.imageCount - 1;
@@ -495,7 +557,7 @@ - (void)scrollWithDelta:(CGFloat)scrollDelta duration:(NSTimeInterval)duration {
viewBeingDragged.center = newViewCenter;
}
completion:^(BOOL finished) {
- NSUInteger newRow = newViewCenter.y / rowHeight;
+ NSUInteger newRow = (isHorizontal ? newViewCenter.x : newViewCenter.y) / rowHeight;
[imageViews removeObject:viewBeingDragged];
[imageViews insertObject:viewBeingDragged atIndex:newRow];
[self setNeedsLayout];
@@ -559,19 +621,39 @@ - (CGRect)frameOfImageAtIndex:(NSUInteger)anIndex {
}
- (CGRect)imageViewFrameInScrollViewForIndex:(NSUInteger)anIndex {
- CGFloat rowWidth = _scrollView.bounds.size.width;
- CGFloat imageViewWidth = rowWidth * 3.0 / 4.0;
- CGFloat imageViewHeight = rowHeight * 3.0 / 4.0;
-
- CGFloat imageOriginX = (rowWidth - imageViewWidth) / 2.0;
- CGFloat imageOriginY = (rowHeight - imageViewHeight) / 2.0;
+ if (isHorizontal == NO) {
+ CGFloat rowWidth = _scrollView.bounds.size.width;
+ CGFloat imageViewWidth = rowWidth * 3.0 / 4.0;
+ CGFloat imageViewHeight = rowHeight * 3.0 / 4.0;
- return CGRectMake(imageOriginX, rowHeight*anIndex + imageOriginY, imageViewWidth, imageViewHeight);
+ CGFloat imageOriginX = (rowWidth - imageViewWidth) / 2.0;
+ CGFloat imageOriginY = (rowHeight - imageViewHeight) / 2.0;
+
+ return CGRectMake(imageOriginX, rowHeight*anIndex + imageOriginY, imageViewWidth, imageViewHeight);
+ }
+ else {
+ CGFloat rowTallness = _scrollView.bounds.size.height;
+ CGFloat imageViewHeight = rowTallness * 3.0 / 4.0;
+ CGFloat imageViewWidth = rowHeight * 3.0 / 4.0;
+
+ CGFloat imageOriginX = (rowTallness - imageViewWidth) / 2.0;
+ CGFloat imageOriginY = (rowHeight - imageViewHeight) / 2.0;
+
+ return CGRectMake(rowHeight*anIndex + imageOriginX, imageOriginY, imageViewWidth, imageViewHeight);
+ }
}
- (CGPoint)imageViewCenterInScrollViewForIndex:(NSUInteger)anIndex {
- CGFloat imageViewCenterX = CGRectGetMidX(_scrollView.bounds);
- CGFloat imageViewCenterY = rowHeight * anIndex + (rowHeight / 2.0);
+ CGFloat imageViewCenterX = 0;
+ CGFloat imageViewCenterY = 0;
+ if (isHorizontal == NO) {
+ imageViewCenterX = CGRectGetMidX(_scrollView.bounds);
+ imageViewCenterY = rowHeight * anIndex + (rowHeight / 2.0);
+ }
+ else {
+ imageViewCenterX = rowHeight * anIndex + (rowHeight / 2.0);
+ imageViewCenterY = CGRectGetMidY(_scrollView.bounds);
+ }
return CGPointMake(imageViewCenterX, imageViewCenterY);
}
@@ -581,8 +663,16 @@ - (BOOL)imageAtIndexIsVisible:(NSUInteger)anIndex {
}
- (NSIndexSet *)visibleIndices {
- NSInteger firstRow = _scrollView.contentOffset.y / rowHeight;
- NSInteger lastRow = (CGRectGetMaxY(_scrollView.bounds)) / rowHeight;
+ NSInteger firstRow = 0;
+ NSInteger lastRow = 0;
+ if (isHorizontal == NO) {
+ firstRow = _scrollView.contentOffset.y / rowHeight;
+ lastRow = (CGRectGetMaxY(_scrollView.bounds)) / rowHeight;
+ }
+ else {
+ firstRow = _scrollView.contentOffset.x / rowHeight;
+ lastRow = (CGRectGetMaxX(_scrollView.bounds)) / rowHeight;
+ }
NSInteger imageCount = self.imageCount;
if (lastRow > imageCount - 1 || imageCount == 0) {
lastRow = imageCount - 1;
@@ -129,7 +129,7 @@ -(void)sidebar:(HSSidebarView *)sidebar didTapImageAtIndex:(NSUInteger)anIndex {
controller.popoverContentSize = content.view.frame.size;
controller.delegate = self;
CGRect frame = [sidebar frameOfImageAtIndex:anIndex];
- [controller presentPopoverFromRect:frame inView:sidebar permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
+ [controller presentPopoverFromRect:frame inView:sidebar permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
self.popover = controller;
[content release];
@@ -35,7 +35,7 @@
B71E114B129BA7870007B0BE /* Green.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Green.png; sourceTree = "<group>"; };
B71E114C129BA7870007B0BE /* Blue.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Blue.png; sourceTree = "<group>"; };
B71E114D129BA7870007B0BE /* Red.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Red.png; sourceTree = "<group>"; };
- B77397AB12A4E85C00CE0A4C /* DeleteImagePopoverController.h */ = {isa = PBXFileReference; fileEncoding = 4; path = DeleteImagePopoverController.h; sourceTree = "<group>"; };
+ B77397AB12A4E85C00CE0A4C /* DeleteImagePopoverController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeleteImagePopoverController.h; sourceTree = "<group>"; };
B77397AC12A4E85C00CE0A4C /* DeleteImagePopoverController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeleteImagePopoverController.m; sourceTree = "<group>"; };
B77397AD12A4E85C00CE0A4C /* DeleteImagePopoverController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = DeleteImagePopoverController.xib; sourceTree = "<group>"; };
B7B8050C129393C300EBE69D /* Sidebar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Sidebar.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -30,7 +30,7 @@
<object class="IBUIView" id="752737248">
<reference key="NSNextResponder" ref="766721923"/>
<int key="NSvFlags">276</int>
- <string key="NSFrameSize">{85, 1004}</string>
+ <string key="NSFrameSize">{768, 85}</string>
<reference key="NSSuperview" ref="766721923"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="798373514"/>

0 comments on commit 522f159

Please sign in to comment.