diff --git a/KNPathTableViewController/KNInfoLabel.h b/KNPathTableViewController/KNInfoLabel.h new file mode 100644 index 0000000..5ccbaa2 --- /dev/null +++ b/KNPathTableViewController/KNInfoLabel.h @@ -0,0 +1,24 @@ +// +// KNInfoLabel.h +// KNPathTableViewControllerDemo +// +// Created by Toan Nguyen Huy on 16/1/12. +// Copyright (c) 2012 Anideo. All rights reserved. +// + +#import + +#define KNPathTableFadeInDuration 0.3 +#define KNPathTableFadeOutDuration 0.3 +#define KNPathTableFadeOutDelay 0.5 +#define KNPathTableResizeDuration 0.2 +#define KNPathTableSlideInOffset 16.0 + +@interface KNInfoLabel : UIView{ + UIImageView * __bgView; + UILabel * __titleLabel; +} + +@property (nonatomic, retain) NSString *title; + +@end diff --git a/KNPathTableViewController/KNInfoLabel.m b/KNPathTableViewController/KNInfoLabel.m new file mode 100644 index 0000000..27708f1 --- /dev/null +++ b/KNPathTableViewController/KNInfoLabel.m @@ -0,0 +1,59 @@ +// +// KN__titleLabel.m +// KNPathTableViewControllerDemo +// +// Created by Toan Nguyen Huy on 16/1/12. +// Copyright (c) 2012 Anideo. All rights reserved. +// + +#import "KNInfoLabel.h" + + +@implementation KNInfoLabel + +@synthesize title; + +- (id)init +{ + self = [super initWithFrame:CGRectMake(0, 0, 150, 32)]; + if (self) { + __bgView = [[UIImageView alloc] initWithFrame:self.bounds]; + UIImage * overlay = [UIImage imageNamed:@"KNTableOverlay"]; + __bgView.image = [overlay stretchableImageWithLeftCapWidth:overlay.size.width/2.0 + topCapHeight:overlay.size.height/2.0]; + + [self addSubview:__bgView]; + + __titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 4, 140, 20)]; + __titleLabel.font = [UIFont boldSystemFontOfSize:12]; + __titleLabel.textAlignment = UITextAlignmentLeft; + __titleLabel.textColor = [UIColor whiteColor]; + __titleLabel.shadowColor = [UIColor blackColor]; + __titleLabel.backgroundColor = [UIColor clearColor]; + __titleLabel.shadowOffset = CGSizeMake(0, -1); + [self addSubview:__titleLabel]; + } + return self; +} + +- (NSString *) getTitle{ + return __titleLabel.text; +} + +- (void) setTitle:(NSString *)newTitle{ + if (newTitle) { + __titleLabel.text = newTitle; + + CGSize textSize = [newTitle sizeWithFont:__titleLabel.font]; + CGSize newSize = CGSizeMake(textSize.width + 35, textSize.height); + + self.frame = CGRectMake(-newSize.width, + self.frame.origin.y, + newSize.width, + self.frame.size.height); + __bgView.frame = self.bounds; + } +} + + +@end diff --git a/KNPathTableViewController/KNPathTableViewController.h b/KNPathTableViewController/KNPathTableViewController.h index 4c9c2cf..3c51c53 100644 --- a/KNPathTableViewController/KNPathTableViewController.h +++ b/KNPathTableViewController/KNPathTableViewController.h @@ -19,22 +19,15 @@ // #import - -#define KNPathTableFadeInDuration 0.3 -#define KNPathTableFadeOutDuration 0.3 -#define KNPathTableFadeOutDelay 0.5 -#define KNPathTableSlideInOffset 16.0 -#define KNPathTableOverlayDefaultSize CGSizeMake(150, 32) +#import "KNInfoLabel.h" @interface KNPathTableViewController : UIViewController { - UIView * __infoPanel; - CGSize __infoPanelSize; - CGRect __infoPanelInitialFrame; + KNInfoLabel * __infoPanel; CGFloat __initalScrollIndicatorHeight; UITableView * __tableView; } -@property (nonatomic,readonly) UIView * infoPanel; +@property (retain) KNInfoLabel * infoPanel; @property (nonatomic,readonly) UITableView * tableView; -(id)initWithStyle:(UITableViewStyle)style; diff --git a/KNPathTableViewController/KNPathTableViewController.m b/KNPathTableViewController/KNPathTableViewController.m index ed6928c..a16bfc9 100644 --- a/KNPathTableViewController/KNPathTableViewController.m +++ b/KNPathTableViewController/KNPathTableViewController.m @@ -25,13 +25,7 @@ @implementation KNPathTableViewController #pragma mark - Custom init -(id)initWithStyle:(UITableViewStyle)style { - if ((self = [self initWithStyle:style infoPanelSize:KNPathTableOverlayDefaultSize])) { - } - return self; -} - --(id)initWithStyle:(UITableViewStyle)style infoPanelSize:(CGSize)size { - if ((self = [super init])) { + if ((self = [self init])) { // The tableview __tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:style]; __tableView.delegate = self; @@ -39,16 +33,9 @@ -(id)initWithStyle:(UITableViewStyle)style infoPanelSize:(CGSize)size { [self.view addSubview:__tableView]; // The panel - __infoPanelSize = size; - __infoPanelInitialFrame = CGRectMake(-__infoPanelSize.width, 0, __infoPanelSize.width, __infoPanelSize.height); - __infoPanel = [[UIView alloc] initWithFrame:__infoPanelInitialFrame]; - - // Initialize overlay panel with stretchable background - UIImageView * bg = [[UIImageView alloc] initWithFrame:__infoPanel.bounds]; - UIImage * overlay = [UIImage imageNamed:@"KNTableOverlay"]; - bg.image = [overlay stretchableImageWithLeftCapWidth:overlay.size.width/2.0 topCapHeight:overlay.size.height/2.0]; - [__infoPanel setAlpha:0]; - [__infoPanel addSubview:bg]; + __infoPanel = [[KNInfoLabel alloc] init]; + __infoPanel.title = @"Hehe"; + __infoPanel.alpha = 0; } return self; } @@ -87,7 +74,7 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // Add it to indicator [self moveInfoPanelToIndicatorView]; - // Prepare to slide in + // Prepare to slide in CGRect f = __infoPanel.frame; CGRect f2= f; f2.origin.x += KNPathTableSlideInOffset; @@ -97,12 +84,12 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self infoPanelWillAppear:scrollView]; [UIView animateWithDuration:KNPathTableFadeInDuration animations:^{ - __infoPanel.alpha = 1; - __infoPanel.frame = f; - } completion:^(BOOL finished) { - [self infoPanelDidAppear:scrollView]; - }]; - } + __infoPanel.alpha = 1; + __infoPanel.frame = f; + } completion:^(BOOL finished) { + [self infoPanelDidAppear:scrollView]; + }]; + } // If it is waiting to fade out, then maintain position else if ([__infoPanel superview] == self.view) { @@ -160,7 +147,7 @@ -(void)moveInfoPanelToSuperView { -(void)moveInfoPanelToIndicatorView { UIView * indicator = [[self.tableView subviews] lastObject]; - CGRect f = __infoPanelInitialFrame; + CGRect f = CGRectMake(-__infoPanel.frame.size.width, 0, __infoPanel.frame.size.width, __infoPanel.frame.size.height); f.origin.y = indicator.frame.size.height/2 - f.size.height/2; if ([__infoPanel superview]) [__infoPanel removeFromSuperview]; [indicator addSubview:__infoPanel]; @@ -193,12 +180,12 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; - + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } - + return cell; } diff --git a/KNPathTableViewControllerDemo.xcodeproj/project.pbxproj b/KNPathTableViewControllerDemo.xcodeproj/project.pbxproj index 07ca041..dfde7ea 100644 --- a/KNPathTableViewControllerDemo.xcodeproj/project.pbxproj +++ b/KNPathTableViewControllerDemo.xcodeproj/project.pbxproj @@ -13,17 +13,14 @@ 376B0CFB14BF18800049756D /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 376B0CF914BF18800049756D /* InfoPlist.strings */; }; 376B0CFD14BF18800049756D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 376B0CFC14BF18800049756D /* main.m */; }; 376B0D0114BF18800049756D /* KNAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 376B0D0014BF18800049756D /* KNAppDelegate.m */; }; - 376B0D0614BF18800049756D /* first.png in Resources */ = {isa = PBXBuildFile; fileRef = 376B0D0514BF18800049756D /* first.png */; }; - 376B0D0814BF18800049756D /* first@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 376B0D0714BF18800049756D /* first@2x.png */; }; 376B0D0B14BF18800049756D /* KNSecondViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 376B0D0A14BF18800049756D /* KNSecondViewController.m */; }; - 376B0D0D14BF18800049756D /* second.png in Resources */ = {isa = PBXBuildFile; fileRef = 376B0D0C14BF18800049756D /* second.png */; }; - 376B0D0F14BF18800049756D /* second@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 376B0D0E14BF18800049756D /* second@2x.png */; }; 376B0D1514BF18800049756D /* KNSecondViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 376B0D1314BF18800049756D /* KNSecondViewController.xib */; }; 376B0D2814BF1C6A0049756D /* KNPathTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 376B0D2514BF1C6A0049756D /* KNPathTableViewController.m */; }; 376B0D2914BF1C6A0049756D /* KNTableOverlay.png in Resources */ = {isa = PBXBuildFile; fileRef = 376B0D2614BF1C6A0049756D /* KNTableOverlay.png */; }; 376B0D2A14BF1C6A0049756D /* KNTableOverlay@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 376B0D2714BF1C6A0049756D /* KNTableOverlay@2x.png */; }; 376B0D2E14BF1D400049756D /* KNFirstViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 376B0D2D14BF1D3F0049756D /* KNFirstViewController.m */; }; 376B0D3814BF54F80049756D /* KNThirdViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 376B0D3714BF54F80049756D /* KNThirdViewController.m */; }; + CF84700914C36A980099DEA5 /* KNInfoLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = CF84700814C36A980099DEA5 /* KNInfoLabel.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -37,12 +34,8 @@ 376B0CFE14BF18800049756D /* KNPathTableViewControllerDemo-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "KNPathTableViewControllerDemo-Prefix.pch"; sourceTree = ""; }; 376B0CFF14BF18800049756D /* KNAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KNAppDelegate.h; sourceTree = ""; }; 376B0D0014BF18800049756D /* KNAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KNAppDelegate.m; sourceTree = ""; }; - 376B0D0514BF18800049756D /* first.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = first.png; sourceTree = ""; }; - 376B0D0714BF18800049756D /* first@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "first@2x.png"; sourceTree = ""; }; 376B0D0914BF18800049756D /* KNSecondViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KNSecondViewController.h; sourceTree = ""; }; 376B0D0A14BF18800049756D /* KNSecondViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KNSecondViewController.m; sourceTree = ""; }; - 376B0D0C14BF18800049756D /* second.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = second.png; sourceTree = ""; }; - 376B0D0E14BF18800049756D /* second@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "second@2x.png"; sourceTree = ""; }; 376B0D1414BF18800049756D /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/KNSecondViewController.xib; sourceTree = ""; }; 376B0D2414BF1C6A0049756D /* KNPathTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KNPathTableViewController.h; sourceTree = ""; }; 376B0D2514BF1C6A0049756D /* KNPathTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KNPathTableViewController.m; sourceTree = ""; }; @@ -52,6 +45,8 @@ 376B0D2D14BF1D3F0049756D /* KNFirstViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KNFirstViewController.m; sourceTree = ""; }; 376B0D3614BF54F80049756D /* KNThirdViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KNThirdViewController.h; sourceTree = ""; }; 376B0D3714BF54F80049756D /* KNThirdViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KNThirdViewController.m; sourceTree = ""; }; + CF84700714C36A980099DEA5 /* KNInfoLabel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KNInfoLabel.h; sourceTree = ""; }; + CF84700814C36A980099DEA5 /* KNInfoLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KNInfoLabel.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -99,10 +94,6 @@ 376B0CF614BF18800049756D /* KNPathTableViewControllerDemo */ = { isa = PBXGroup; children = ( - 376B0D0514BF18800049756D /* first.png */, - 376B0D0714BF18800049756D /* first@2x.png */, - 376B0D0C14BF18800049756D /* second.png */, - 376B0D0E14BF18800049756D /* second@2x.png */, 376B0CFF14BF18800049756D /* KNAppDelegate.h */, 376B0D0014BF18800049756D /* KNAppDelegate.m */, 376B0D2B14BF1D0F0049756D /* KNFirstViewController.h */, @@ -135,6 +126,8 @@ 376B0D2514BF1C6A0049756D /* KNPathTableViewController.m */, 376B0D2614BF1C6A0049756D /* KNTableOverlay.png */, 376B0D2714BF1C6A0049756D /* KNTableOverlay@2x.png */, + CF84700714C36A980099DEA5 /* KNInfoLabel.h */, + CF84700814C36A980099DEA5 /* KNInfoLabel.m */, ); path = KNPathTableViewController; sourceTree = ""; @@ -191,10 +184,6 @@ buildActionMask = 2147483647; files = ( 376B0CFB14BF18800049756D /* InfoPlist.strings in Resources */, - 376B0D0614BF18800049756D /* first.png in Resources */, - 376B0D0814BF18800049756D /* first@2x.png in Resources */, - 376B0D0D14BF18800049756D /* second.png in Resources */, - 376B0D0F14BF18800049756D /* second@2x.png in Resources */, 376B0D1514BF18800049756D /* KNSecondViewController.xib in Resources */, 376B0D2914BF1C6A0049756D /* KNTableOverlay.png in Resources */, 376B0D2A14BF1C6A0049756D /* KNTableOverlay@2x.png in Resources */, @@ -214,6 +203,7 @@ 376B0D2814BF1C6A0049756D /* KNPathTableViewController.m in Sources */, 376B0D2E14BF1D400049756D /* KNFirstViewController.m in Sources */, 376B0D3814BF54F80049756D /* KNThirdViewController.m in Sources */, + CF84700914C36A980099DEA5 /* KNInfoLabel.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/KNPathTableViewControllerDemo/KNFirstViewController.h b/KNPathTableViewControllerDemo/KNFirstViewController.h index b3e70f5..45de68c 100644 --- a/KNPathTableViewControllerDemo/KNFirstViewController.h +++ b/KNPathTableViewControllerDemo/KNFirstViewController.h @@ -11,7 +11,6 @@ @interface KNFirstViewController : KNPathTableViewController { NSMutableArray * array; - UILabel * infoLabel; } @end diff --git a/KNPathTableViewControllerDemo/KNFirstViewController.m b/KNPathTableViewControllerDemo/KNFirstViewController.m index 1f9084e..f5fe866 100644 --- a/KNPathTableViewControllerDemo/KNFirstViewController.m +++ b/KNPathTableViewControllerDemo/KNFirstViewController.m @@ -19,7 +19,15 @@ -(id)initWithStyle:(UITableViewStyle)style { // Add a series of number array = [[NSMutableArray alloc] init]; for (int k=0;k<100;k++) { - [array addObject:[NSString stringWithFormat:@"Test row number %d", k]]; + if(k < 30){ + [array addObject:[NSString stringWithFormat:@"Row %d", k]]; + } + else if(k < 60) { + [array addObject:[NSString stringWithFormat:@"Longer row %d", k]]; + } + else { + [array addObject:[NSString stringWithFormat:@"Even longer row %d", k]]; + } } } return self; @@ -29,23 +37,16 @@ -(id)initWithStyle:(UITableViewStyle)style { -(void)viewDidLoad { [super viewDidLoad]; - infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 4, 140, 20)]; - infoLabel.font = [UIFont boldSystemFontOfSize:12]; - infoLabel.textAlignment = UITextAlignmentLeft; - infoLabel.textColor = [UIColor whiteColor]; - infoLabel.shadowColor = [UIColor blackColor]; - infoLabel.backgroundColor = [UIColor clearColor]; - infoLabel.shadowOffset = CGSizeMake(0, 1); + // [self.infoPanel addSubview:infoLabel] // this will not work, too early to add } -(void)infoPanelWillAppear:(UIScrollView *)scrollView { - if (![infoLabel superview]) [self.infoPanel addSubview:infoLabel]; } -(void)infoPanelDidScroll:(UIScrollView *)scrollView atPoint:(CGPoint)point { NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point]; - infoLabel.text = [NSString stringWithFormat:@"Something about %d", indexPath.row]; + self.infoPanel.title = [__tableView cellForRowAtIndexPath:indexPath].textLabel.text; } #pragma mark - Give table some content @@ -66,7 +67,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } + cell.textLabel.text = [array objectAtIndex:indexPath.row]; + + return cell; } diff --git a/KNPathTableViewControllerDemo/KNThirdViewController.m b/KNPathTableViewControllerDemo/KNThirdViewController.m index 3ee70e1..e6ffed6 100644 --- a/KNPathTableViewControllerDemo/KNThirdViewController.m +++ b/KNPathTableViewControllerDemo/KNThirdViewController.m @@ -29,7 +29,7 @@ -(id)initWithStyle:(UITableViewStyle)style { -(void)infoPanelDidScroll:(UIScrollView *)scrollView atPoint:(CGPoint)point { NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point]; - infoLabel.text = [NSString stringWithFormat:@"In section %d", indexPath.section+1]; + self.infoPanel.title = [NSString stringWithFormat:@"In section %d", indexPath.section+1]; } #pragma mark - Give table some content diff --git a/KNPathTableViewControllerDemo/first.png b/KNPathTableViewControllerDemo/first.png deleted file mode 100644 index 9300ee2..0000000 Binary files a/KNPathTableViewControllerDemo/first.png and /dev/null differ diff --git a/KNPathTableViewControllerDemo/first@2x.png b/KNPathTableViewControllerDemo/first@2x.png deleted file mode 100644 index 374d78e..0000000 Binary files a/KNPathTableViewControllerDemo/first@2x.png and /dev/null differ diff --git a/KNPathTableViewControllerDemo/second.png b/KNPathTableViewControllerDemo/second.png deleted file mode 100644 index 1100b48..0000000 Binary files a/KNPathTableViewControllerDemo/second.png and /dev/null differ diff --git a/KNPathTableViewControllerDemo/second@2x.png b/KNPathTableViewControllerDemo/second@2x.png deleted file mode 100644 index 368d38f..0000000 Binary files a/KNPathTableViewControllerDemo/second@2x.png and /dev/null differ