Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finalized index view using tracking from the global gesture recognizer.

  • Loading branch information...
commit 91e1ac1cc010c80ee887aa1246d0e2f7c606aebd 1 parent ff9bd35
@blommegard blommegard authored
View
5 Examples/GridViewDemo/GridViewDemo/GridViewDemoViewController.m
@@ -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)];
@@ -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
View
27 KKGridView/KKGridView.m
@@ -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;
@@ -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;
@@ -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
View
3  KKGridView/KKGridViewIndexView.h
@@ -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
View
61 KKGridView/KKGridViewIndexView.m
@@ -11,27 +11,25 @@
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];
}
}
@@ -39,32 +37,11 @@ - (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
@@ -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)];
}
@@ -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;
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.