Skip to content

Commit

Permalink
Smooth title animations
Browse files Browse the repository at this point in the history
  • Loading branch information
graetzer committed Jun 14, 2012
1 parent aabb537 commit 5ef2b5b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 68 deletions.
8 changes: 6 additions & 2 deletions Demo/SGTabs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<group>"; };
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; };
Expand Down Expand Up @@ -92,6 +93,7 @@
isa = PBXGroup;
children = (
F470D27E1580EA5100ED3644 /* UIKit.framework */,
F422133E158A8B310065C7CC /* CoreText.framework */,
F470D2801580EA5100ED3644 /* Foundation.framework */,
F470D2821580EA5100ED3644 /* CoreGraphics.framework */,
);
Expand Down Expand Up @@ -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";
};
Expand All @@ -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";
Expand All @@ -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;
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion Demo/SGTabs/SGTabs-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
Expand Down
6 changes: 3 additions & 3 deletions Demo/SGTabs/SGViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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];

}
Expand Down
28 changes: 2 additions & 26 deletions Demo/SGTabs/en.lproj/SGViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<int key="NSvFlags">274</int>
<string key="NSFrame">{{77, 159}, {167, 142}}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="IBUIOpaque">NO</bool>
Expand Down Expand Up @@ -70,7 +69,6 @@
</array>
<string key="NSFrame">{{0, 20}, {320, 460}}</string>
<reference key="NSSuperview"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="638580792"/>
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">3</int>
Expand Down Expand Up @@ -146,31 +144,9 @@
<nil key="activeLocalization"/>
<dictionary class="NSMutableDictionary" key="localizations"/>
<nil key="sourceID"/>
<int key="maxID">10</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
<object class="IBPartialClassDescription">
<string key="className">SGViewController</string>
<string key="superclassName">UIViewController</string>
<object class="NSMutableDictionary" key="outlets">
<string key="NS.key.0">label</string>
<string key="NS.object.0">UILabel</string>
</object>
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
<string key="NS.key.0">label</string>
<object class="IBToOneOutletInfo" key="NS.object.0">
<string key="name">label</string>
<string key="candidateClassName">UILabel</string>
</object>
</object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/SGViewController.h</string>
</object>
</object>
</array>
<int key="maxID">14</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes"/>
<int key="IBDocument.localizationMode">0</int>
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions Source/SGTabView.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@


#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>

@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;
Expand Down
56 changes: 35 additions & 21 deletions Source/SGTabView.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,67 @@

#import <math.h>



@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 {
Expand Down Expand Up @@ -105,6 +116,7 @@ - (void)drawRect:(CGRect)rect {
CGPathCloseSubpath(path);

CGContextRef ctx = UIGraphicsGetCurrentContext();

// Fill with current tab color
CGColorRef startColor = [self.tabColor CGColor];

Expand All @@ -115,6 +127,8 @@ - (void)drawRect:(CGRect)rect {
CGContextFillPath(ctx);
CGContextRestoreGState(ctx);

CGPathRelease(path);

}

@end
4 changes: 1 addition & 3 deletions Source/SGTabsView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:)];
Expand All @@ -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];

Expand Down
10 changes: 3 additions & 7 deletions Source/SGTabsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -291,10 +292,5 @@ - (NSMutableArray *)tabContents {
}
return _tabContents;
}

- (void)addChildViewController:(UIViewController *)childController {
[self.tabContents addObject:childController];
[super addChildViewController:childController];
}

@end

0 comments on commit 5ef2b5b

Please sign in to comment.