From 5ef2b5ba9419200aeee87c181a6a2006b3094111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Gra=CC=88tzer?= Date: Fri, 15 Jun 2012 00:45:11 +0200 Subject: [PATCH] Smooth title animations --- Demo/SGTabs.xcodeproj/project.pbxproj | 8 +++- Demo/SGTabs/SGTabs-Info.plist | 1 - Demo/SGTabs/SGViewController.m | 6 +-- Demo/SGTabs/en.lproj/SGViewController.xib | 28 +----------- README.md | 3 +- Source/SGTabView.h | 9 ++-- Source/SGTabView.m | 56 ++++++++++++++--------- Source/SGTabsView.m | 4 +- Source/SGTabsViewController.m | 10 ++-- 9 files changed, 57 insertions(+), 68 deletions(-) diff --git a/Demo/SGTabs.xcodeproj/project.pbxproj b/Demo/SGTabs.xcodeproj/project.pbxproj index 6d56e31..e37b9a8 100644 --- a/Demo/SGTabs.xcodeproj/project.pbxproj +++ b/Demo/SGTabs.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + F422133E158A8B310065C7CC /* CoreText.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreText.framework; path = System/Library/Frameworks/CoreText.framework; sourceTree = SDKROOT; }; F450946B15856EC800B7B1C6 /* cross.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = cross.png; sourceTree = ""; }; F470D27A1580EA5100ED3644 /* SGTabs.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SGTabs.app; sourceTree = BUILT_PRODUCTS_DIR; }; F470D27E1580EA5100ED3644 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; @@ -92,6 +93,7 @@ isa = PBXGroup; children = ( F470D27E1580EA5100ED3644 /* UIKit.framework */, + F422133E158A8B310065C7CC /* CoreText.framework */, F470D2801580EA5100ED3644 /* Foundation.framework */, F470D2821580EA5100ED3644 /* CoreGraphics.framework */, ); @@ -275,7 +277,7 @@ GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 5.1; + IPHONEOS_DEPLOYMENT_TARGET = 5.0; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; }; @@ -294,7 +296,7 @@ GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 5.1; + IPHONEOS_DEPLOYMENT_TARGET = 5.0; OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; @@ -308,6 +310,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "SGTabs/SGTabs-Prefix.pch"; INFOPLIST_FILE = "SGTabs/SGTabs-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 5.0; PRODUCT_NAME = "$(TARGET_NAME)"; TARGETED_DEVICE_FAMILY = 2; WRAPPER_EXTENSION = app; @@ -320,6 +323,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "SGTabs/SGTabs-Prefix.pch"; INFOPLIST_FILE = "SGTabs/SGTabs-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 5.0; PRODUCT_NAME = "$(TARGET_NAME)"; TARGETED_DEVICE_FAMILY = 2; WRAPPER_EXTENSION = app; diff --git a/Demo/SGTabs/SGTabs-Info.plist b/Demo/SGTabs/SGTabs-Info.plist index df4e79c..377d7ec 100644 --- a/Demo/SGTabs/SGTabs-Info.plist +++ b/Demo/SGTabs/SGTabs-Info.plist @@ -30,7 +30,6 @@ UISupportedInterfaceOrientations - UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight diff --git a/Demo/SGTabs/SGViewController.m b/Demo/SGTabs/SGViewController.m index 925a83e..05cf0d3 100644 --- a/Demo/SGTabs/SGViewController.m +++ b/Demo/SGTabs/SGViewController.m @@ -63,8 +63,8 @@ - (void)viewDidDisappear:(BOOL)animated { - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { - if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { - return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { + return UIInterfaceOrientationIsLandscape(interfaceOrientation); } else { return YES; } @@ -81,7 +81,7 @@ - (IBAction)add:(id)sender { SGViewController *vc = [[SGViewController alloc] initWithNibName:NSStringFromClass([SGViewController class]) bundle:nil]; - vc.title = [NSString stringWithFormat:@"Tab %i content", tabs.count+1]; + vc.title = [NSString stringWithFormat:@"Tab %i contents!", tabs.count+1]; [tabs addTab:vc]; } diff --git a/Demo/SGTabs/en.lproj/SGViewController.xib b/Demo/SGTabs/en.lproj/SGViewController.xib index 5c32c46..1c20713 100644 --- a/Demo/SGTabs/en.lproj/SGViewController.xib +++ b/Demo/SGTabs/en.lproj/SGViewController.xib @@ -40,7 +40,6 @@ 274 {{77, 159}, {167, 142}} - _NS:9 NO @@ -70,7 +69,6 @@ {{0, 20}, {320, 460}} - 3 @@ -146,31 +144,9 @@ - 10 - - - - - SGViewController - UIViewController - - label - UILabel - - - label - - label - UILabel - - - - IBProjectSource - ./Classes/SGViewController.h - - - + 14 + 0 IBCocoaTouchFramework diff --git a/README.md b/README.md index deeaef2..50de87e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ Tab component for iOS 5+. You can show your ViewControllers in tabs, it is possi - User can remove tabs by pressing an X on a tab. TODO - Dynamically show and hide a UIToolbar at the top - Show the UIBarButtonItems in viewController.toolbarItems in the toolbar -- It should be possible to create Safari style apps +- Enables you tobuild mobile Safari style Apps + # Screenshots diff --git a/Source/SGTabView.h b/Source/SGTabView.h index 202d9ac..6e28d0e 100644 --- a/Source/SGTabView.h +++ b/Source/SGTabView.h @@ -22,12 +22,13 @@ #import +#import -@interface SGTabView : UIView - -@property (nonatomic, strong) UILabel *titleLabel; -@property (nonatomic, strong) UIButton *closeButton; +@interface SGTabView : UIView { + CGSize _tSize; +} +@property (nonatomic, strong) NSString *title; @property (nonatomic, strong) UIColor *tabColor; - (id)initWithFrame:(CGRect)frame title:(NSString *)title; diff --git a/Source/SGTabView.m b/Source/SGTabView.m index 7f32ac7..7b1cc84 100644 --- a/Source/SGTabView.m +++ b/Source/SGTabView.m @@ -25,56 +25,67 @@ #import - - +@interface SGTabView () +@property (nonatomic, strong) UILabel *titleLabel; +@property (nonatomic, strong) UIButton *closeButton; +@end @implementation SGTabView @synthesize titleLabel, closeButton; @synthesize tabColor; - -- (CGRect)tabRect { - return CGRectMake(self.bounds.origin.x, - self.bounds.origin.y, - self.bounds.size.width, - self.bounds.size.height); -} +@dynamic title; - (id)initWithFrame:(CGRect)frame title:(NSString *)title { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; - self.autoresizesSubviews = UIViewAutoresizingFlexibleWidth; - + self.autoresizingMask = UIViewAutoresizingFlexibleWidth; + CGFloat cap = kCornerRadius/frame.size.width; + self.contentStretch = CGRectMake(cap, 0.0, 1.0, 1-cap); + self.tabColor = kTabColor; + self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; - self.titleLabel.text = title; self.titleLabel.textAlignment = UITextAlignmentCenter; self.titleLabel.lineBreakMode = UILineBreakModeTailTruncation; self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; self.titleLabel.backgroundColor = [UIColor clearColor]; self.titleLabel.font = [UIFont boldSystemFontOfSize:14.0]; + self.titleLabel.minimumFontSize = 14.0; self.titleLabel.textColor = [UIColor darkGrayColor]; - self.titleLabel.shadowColor = [UIColor colorWithWhite:1 alpha:0.5]; + self.titleLabel.shadowColor = [UIColor colorWithWhite:0.6 alpha:0.5]; self.titleLabel.shadowOffset = CGSizeMake(0, 0.5); + + self.title = title; [self addSubview:self.titleLabel]; + // self.closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // [self.closeButton setTitle:@"Hello" forState:UIControlStateNormal]; // [self.closeButton setImage:[UIImage imageNamed:@"cross.png"] // forState:UIControlStateNormal]; - [self addSubview:self.closeButton]; - - self.tabColor = kTabColor; +// [self addSubview:self.closeButton]; } return self; } - (void)layoutSubviews { - CGRect inner = [self tabRect]; - self.titleLabel.frame = inner; -// inner.origin.x -= 10; -// inner.size.width -=10; -// self.closeButton.frame = inner; + CGSize t = _tSize; + if (t.width > self.bounds.size.width*0.75) + t.width = self.bounds.size.width*0.75; + CGRect b = self.bounds; + self.titleLabel.frame = CGRectMake((b.size.width - t.width)/2, + (b.size.height - t.height)/2, + t.width, t.height); +} + +- (void)setTitle:(NSString *)title { + self.titleLabel.text = title; + _tSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font]; +} + +- (NSString *)title { + return self.titleLabel.text; } - (void)drawRect:(CGRect)rect { @@ -105,6 +116,7 @@ - (void)drawRect:(CGRect)rect { CGPathCloseSubpath(path); CGContextRef ctx = UIGraphicsGetCurrentContext(); + // Fill with current tab color CGColorRef startColor = [self.tabColor CGColor]; @@ -115,6 +127,8 @@ - (void)drawRect:(CGRect)rect { CGContextFillPath(ctx); CGContextRestoreGState(ctx); + CGPathRelease(path); + } @end diff --git a/Source/SGTabsView.m b/Source/SGTabsView.m index e522789..d0fb04c 100644 --- a/Source/SGTabsView.m +++ b/Source/SGTabsView.m @@ -62,7 +62,7 @@ - (void)addTab:(NSString *)title { CGRect frame = CGRectMake(self.bounds.size.width, 0, width, self.bounds.size.height - kMARGIN); SGTabView *newTab = [[SGTabView alloc] initWithFrame:frame title:title]; - newTab.closeButton.hidden = !self.tabsController.editing; + //newTab.closeButton.hidden = !self.tabsController.editing; UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; @@ -76,8 +76,6 @@ - (void)addTab:(NSString *)title { panG.delegate = self; [newTab addGestureRecognizer:panG]; -// CGFloat cap = 7.5/width; -// newTab.contentStretch = CGRectMake(cap, 0.0, 1.0, 1-cap); _selected = self.tabs.count; [self.tabs addObject:newTab]; diff --git a/Source/SGTabsViewController.m b/Source/SGTabsViewController.m index 3e773ba..1621593 100644 --- a/Source/SGTabsViewController.m +++ b/Source/SGTabsViewController.m @@ -61,7 +61,7 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return YES; else - return (interfaceOrientation == UIInterfaceOrientationPortrait); + return UIInterfaceOrientationIsLandscape(interfaceOrientation);; } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { @@ -113,6 +113,7 @@ - (void)addTab:(UIViewController *)viewController { if (![self.childViewControllers containsObject:viewController] && self.count < self.maxCount - 1) { [self addChildViewController:viewController]; + [self.tabContents addObject:viewController]; viewController.view.frame = _contentFrame; if (_toobarVisible) @@ -278,7 +279,7 @@ - (BOOL)toolbarHidden { #pragma mark - Propertys - (NSUInteger)maxCount { - return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 6 : 3; + return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 8 : 4; } - (NSUInteger)count { @@ -291,10 +292,5 @@ - (NSMutableArray *)tabContents { } return _tabContents; } - -- (void)addChildViewController:(UIViewController *)childController { - [self.tabContents addObject:childController]; - [super addChildViewController:childController]; -} @end