Skip to content

Commit

Permalink
Finalized index view using tracking from the global gesture recognizer.
Browse files Browse the repository at this point in the history
  • Loading branch information
blommegard committed Dec 23, 2011
1 parent ff9bd35 commit 91e1ac1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 51 deletions.
Expand Up @@ -29,7 +29,7 @@ - (void)loadView
searchBar.barStyle = UIBarStyleBlackTranslucent;
searchBar.delegate = self;
searchBar.showsCancelButton = YES;
searchBar.userInteractionEnabled = NO;
searchBar.userInteractionEnabled = YES;
self.gridView.gridHeaderView = searchBar;

UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 20.f, 50.f)];
Expand Down Expand Up @@ -111,6 +111,9 @@ - (NSString *)gridView:(KKGridView *)gridView titleForFooterInSection:(NSUIntege
return [NSString stringWithFormat:@"Footer %i",section];
}

- (NSArray *)sectionIndexTitlesForGridView:(KKGridView *)gridView {
return [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",nil];
}

#pragma mark - UISearchBarDelegate

Expand Down
27 changes: 20 additions & 7 deletions KKGridView/KKGridView.m
Expand Up @@ -386,10 +386,6 @@ - (void)_layoutAccessories
f.origin.y = sectionTwoY + sectionTwoHeight;
}
}

// move footer view to right below scroller
[footer.view removeFromSuperview];
[self insertSubview:footer.view atIndex:self.subviews.count - 1];
} else {
// footer isn't sticky anymore, set originTop to saved position
f.origin.y = footer->stickPoint;
Expand Down Expand Up @@ -1354,7 +1350,25 @@ - (void)_deselectItemAtIndexPath:(KKIndexPath *)indexPath
#pragma mark - Touch Handling

- (void)_handleSelection:(UILongPressGestureRecognizer *)recognizer
{
{
if (_indexView) {
if (CGRectContainsPoint(_indexView.frame, [recognizer locationInView:self]) &&
recognizer.state == UIGestureRecognizerStateBegan) {
[self setScrollEnabled:NO];
[_indexView setTracking:YES location:[recognizer locationInView:_indexView]];
return;
}
else if (recognizer.state == UIGestureRecognizerStateChanged && _indexView.tracking) {
[_indexView setTracking:YES location:CGPointMake(0.0, [recognizer locationInView:_indexView].y)];
return;
}
else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
[self setScrollEnabled:YES];
[_indexView setTracking:NO location:[recognizer locationInView:_indexView]];
return;
}
}

if ([self isDecelerating])
return;

Expand Down Expand Up @@ -1396,12 +1410,11 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (_indexView) {
if (_indexView)
[_indexView setFrame:CGRectMake(_indexView.frame.origin.x,
scrollView.contentOffset.y,
_indexView.frame.size.width,
_indexView.frame.size.height)];
}
}

@end
Expand Down
3 changes: 3 additions & 0 deletions KKGridView/KKGridViewIndexView.h
Expand Up @@ -10,4 +10,7 @@
@interface KKGridViewIndexView : UIView
@property (nonatomic, strong) NSArray *sectionIndexTitles; // Array with NSStrings
@property (nonatomic, copy) void (^sectionTracked)(NSUInteger section);
@property (nonatomic, assign) BOOL tracking;

- (void)setTracking:(BOOL)tracking location:(CGPoint)location;
@end
61 changes: 18 additions & 43 deletions KKGridView/KKGridViewIndexView.m
Expand Up @@ -11,60 +11,37 @@
static UIColor *fontColor = nil;
static UIFont *font = nil;

#define KKGridViewIndexViewPadding 5.
#define KKGridViewIndexViewMargin 5.
#define KKGridViewIndexViewPadding 7.0
#define KKGridViewIndexViewMargin 7.0

@interface KKGridViewIndexView () {
NSUInteger _lastTrackingSection;
BOOL _tracking;
}

- (void)setTracking:(BOOL)tracking touch:(UITouch *)touch;
@end

@implementation KKGridViewIndexView

@synthesize sectionIndexTitles = _sectionIndexTitles;
@synthesize sectionTracked = _sectionTracked;
@synthesize tracking = _tracking;

+ (void)initialize {
if (self == [KKGridViewIndexView class]) {
backgroundColor = [UIColor colorWithWhite:0. alpha:.25];
fontColor = [UIColor colorWithWhite:0. alpha:.75];
font = [UIFont boldSystemFontOfSize:12.];
backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.25];
fontColor = [UIColor colorWithWhite:0.0 alpha:0.75];
font = [UIFont boldSystemFontOfSize:12.0];
}
}

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setOpaque:NO];
[self setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight)];
[self setUserInteractionEnabled:YES];
}

return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Start");
[self setTracking:YES touch:[touches anyObject]];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Moved");
[self setTracking:YES touch:[touches anyObject]];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Cancelled");
[self setTracking:NO touch:[touches anyObject]];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Ended");
[self setTracking:NO touch:[touches anyObject]];
}

- (void)drawRect:(CGRect)rect {

// If tracking, draw background
Expand Down Expand Up @@ -95,7 +72,7 @@ - (void)drawRect:(CGRect)rect {

- (void)willMoveToSuperview:(UIView *)newSuperview {
[self setFrame:CGRectMake(newSuperview.bounds.size.width-self.frame.size.width,
0.,
0.0,
self.frame.size.width,
newSuperview.bounds.size.height)];
}
Expand All @@ -106,8 +83,8 @@ - (void)setSectionIndexTitles:(NSArray *)sectionIndexTitles {
_sectionIndexTitles = sectionIndexTitles;

// Look for the largest title and set the width
CGFloat maxWidth = 0.;
CGFloat currentWidth = 0.;
CGFloat maxWidth = 0.0;
CGFloat currentWidth = 0.0;
for (NSString *title in _sectionIndexTitles) {
currentWidth = [title sizeWithFont:font].width;

Expand All @@ -123,25 +100,23 @@ - (void)setSectionIndexTitles:(NSArray *)sectionIndexTitles {
[self setNeedsDisplay];
}

#pragma mark - Private
#pragma mark - Public

- (void)setTracking:(BOOL)tracking touch:(UITouch *)touch {
- (void)setTracking:(BOOL)tracking location:(CGPoint)location {
_tracking = tracking;
if (_tracking) {
NSUInteger sections = [_sectionIndexTitles count];
CGFloat sectionHeight = (self.bounds.size.height - 2*KKGridViewIndexViewMargin)/sections;
CGPoint point = [touch locationInView:self];
point.y-=KKGridViewIndexViewMargin;
location.y-=KKGridViewIndexViewMargin;

NSUInteger trackingSection = floorf(point.y/sectionHeight);
if (trackingSection != _lastTrackingSection) {
_lastTrackingSection = trackingSection;

if (_sectionTracked)
_sectionTracked(_lastTrackingSection);
}
_lastTrackingSection = floorf(abs(location.y)/sectionHeight);

if (_sectionTracked)
_sectionTracked(_lastTrackingSection);

}
[self setNeedsDisplay];
}


@end

0 comments on commit 91e1ac1

Please sign in to comment.