Permalink
Browse files

Added CKSideBarItem to ease glow selected/unselected image behavior

  • Loading branch information...
1 parent 054fea1 commit c9347cd7f21e21727c5711a1ccc679db8496326e @jaykz52 committed Nov 25, 2012
@@ -11,6 +11,7 @@
6039020135B7FE399ECC8581 /* compose.png in Resources */ = {isa = PBXBuildFile; fileRef = 6039009C327E05AD11B04DD3 /* compose.png */; };
603902A70EDD557C51285919 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 60390F3BC828FC41D1CDF940 /* Default-568h@2x.png */; };
603902FED5235533FB269F60 /* compose@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 60390B81AEBE479629D158E3 /* compose@2x.png */; };
+ 60390384643D87D83E27C702 /* CKSideBarItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 60390C602C60C243009B68C2 /* CKSideBarItem.m */; };
60390401971DE67BFF33D6A8 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 60390FF173C1E692E933530D /* QuartzCore.framework */; };
603904D70F8697EBD456D01A /* selected-image-background@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 603902398B1B3B59F7634823 /* selected-image-background@2x.png */; };
6039057337996A8C74B04646 /* CKAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 603902D4BE74F217BB3CBDEA /* CKAppDelegate.m */; };
@@ -49,6 +50,7 @@
603903179ABBE8CBA3418D10 /* rough_diagonal.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = rough_diagonal.png; path = ../../Source/Resources/rough_diagonal.png; sourceTree = "<group>"; };
60390357DCE9834B33EA962E /* search.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = search.png; sourceTree = "<group>"; };
603903CEA8D8FAF677D8EA9E /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = "<group>"; };
+ 60390531E6989CC7FBFCB471 /* CKSideBarItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CKSideBarItem.h; path = ../../Source/CKSideBarItem.h; sourceTree = "<group>"; };
603905C129889BF6A9A7DDFC /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
603905CD1AA2671B175D67F5 /* search@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "search@2x.png"; sourceTree = "<group>"; };
603905EA633E4D05343B0832 /* messages@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "messages@2x.png"; sourceTree = "<group>"; };
@@ -62,6 +64,7 @@
60390A152B5584589231999E /* CKSideBarController-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.info; path = "CKSideBarController-Info.plist"; sourceTree = "<group>"; };
60390A626D668E03CF89A800 /* CKSideBarController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CKSideBarController.m; path = ../../Source/CKSideBarController.m; sourceTree = "<group>"; };
60390B81AEBE479629D158E3 /* compose@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "compose@2x.png"; sourceTree = "<group>"; };
+ 60390C602C60C243009B68C2 /* CKSideBarItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CKSideBarItem.m; path = ../../Source/CKSideBarItem.m; sourceTree = "<group>"; };
60390D4C57AD386112E2E314 /* CKAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CKAppDelegate.h; sourceTree = "<group>"; };
60390ED1B9F265F8CD8E9889 /* messages.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = messages.png; sourceTree = "<group>"; };
60390F3BC828FC41D1CDF940 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; };
@@ -124,6 +127,8 @@
60390B81AEBE479629D158E3 /* compose@2x.png */,
60390357DCE9834B33EA962E /* search.png */,
603905CD1AA2671B175D67F5 /* search@2x.png */,
+ 60390531E6989CC7FBFCB471 /* CKSideBarItem.h */,
+ 60390C602C60C243009B68C2 /* CKSideBarItem.m */,
);
path = CKSideBarController;
sourceTree = "<group>";
@@ -242,6 +247,7 @@
6039057337996A8C74B04646 /* CKAppDelegate.m in Sources */,
60390F9634B4E3583978D86E /* CKSideBarController.m in Sources */,
60390E5B75EBB4EC204BC425 /* CKTestViewController.m in Sources */,
+ 60390384643D87D83E27C702 /* CKSideBarItem.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -30,33 +30,42 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
- self.controller1 = [self navControllerWithTitle:@"Tweets" backgroundColor:[UIColor whiteColor] barImage:nil];
- self.controller2 = [self navControllerWithTitle:@"Connect" backgroundColor:[UIColor whiteColor] barImage:[UIImage imageNamed:@"compose.png"]];
- self.controller3 = [self navControllerWithTitle:@"Can't Touch" backgroundColor:[UIColor whiteColor] barImage:[UIImage imageNamed:@"search.png"]];
-
+ self.controller1 = [self navControllerWithTitle:@"Tweets" barImage:nil];
+ self.controller2 = [self navControllerWithTitle:@"Connect" barImage:[UIImage imageNamed:@"compose.png"]];
+ self.controller3 = [self navControllerWithTitle:@"Can't Touch" barImage:[UIImage imageNamed:@"search.png"]];
+
+ UIViewController *detachedController = [[UIViewController alloc] init];
+ NSLog(@"side bar controller should be nil: %@", detachedController.sideBarController);
+
self.barController = [[CKSideBarController alloc] init];
self.barController.delegate = self;
self.barController.viewControllers = @[
self.controller1,
self.controller2,
self.controller3
];
+
+ NSLog(@"side bar controller should not be nil: %@", self.controller1.sideBarController);
+
self.window.rootViewController = self.barController;
return YES;
}
-BOOL setTabBarTitle = YES;
-- (UINavigationController *)navControllerWithTitle:(NSString *)title backgroundColor:(UIColor *)backgroundColor barImage:(UIImage *)image {
+BOOL shouldAlternate = YES;
+
+- (UINavigationController *)navControllerWithTitle:(NSString *)title barImage:(UIImage *)image {
CKTestViewController *controller = [[CKTestViewController alloc] init];
controller.title = title;
- controller.view.backgroundColor = backgroundColor;
+ controller.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
- if (setTabBarTitle)
- navController.tabBarItem.title = title;
- navController.tabBarItem.image = image;
+ if (shouldAlternate)
+ navController.sideBarItem.title = title;
+ if (image)
+ navController.sideBarItem.image = image;
+ navController.sideBarItem.isGlowing = shouldAlternate;
- setTabBarTitle = !setTabBarTitle;
+ shouldAlternate = !shouldAlternate;
return navController;
}
@@ -111,9 +120,4 @@ - (void)sideBarController:(CKSideBarController *)sideBarController didSelectView
NSLog(@"viewController %@ selected!", viewController);
}
-- (BOOL)sideBarController:(CKSideBarController *)sideBarController rowShouldGlow:(NSUInteger)row {
- return row == 2;
-}
-
-
@end
@@ -16,25 +16,36 @@
//
#import <Foundation/Foundation.h>
+#import "CKSideBarItem.h"
@protocol CKSideBarControllerDelegate;
+
@interface CKSideBarController : UIViewController
@property (nonatomic, copy) NSArray *viewControllers;
-
@property (nonatomic, assign) UIViewController *selectedViewController;
@property (nonatomic) NSUInteger selectedIndex;
-
@property (nonatomic, assign) id<CKSideBarControllerDelegate> delegate;
+// TODO: get rid of this method (KVO on sideBarItem on managed viewControllers)
+- (void)refresh;
+
@end
+
@protocol CKSideBarControllerDelegate <NSObject>
@optional
- (BOOL)sideBarController:(CKSideBarController *)sideBarController shouldSelectViewController:(UIViewController *)viewController;
- (void)sideBarController:(CKSideBarController *)sideBarController didSelectViewController:(UIViewController *)viewController;
-- (BOOL)sideBarController:(CKSideBarController *)sideBarController rowShouldGlow:(NSUInteger)row;
-@end
+@end
+
+
+@interface UIViewController (CKSideBarController)
+
+@property (nonatomic, readonly, retain) CKSideBarController *sideBarController; // If the view controller has a side bar controller as its ancestor, return it. Returns nil otherwise.
+@property (nonatomic) CKSideBarItem *sideBarItem;
+
+@end
@@ -16,6 +16,7 @@
//
#import <QuartzCore/QuartzCore.h>
+#import <objc/runtime.h>
#import "CKSideBarController.h"
#define CKSideBarWidth 84
@@ -27,7 +28,8 @@
@interface CKSideBarCell : UITableViewCell
@property (nonatomic) UIImageView *iconView;
-@property (nonatomic) UIImage *image;
+@property (nonatomic) UIImage *selectedImage;
+@property (nonatomic) UIImage *unSelectedImage;
@property (nonatomic) UILabel *titleLabel;
@property (nonatomic) UIImageView *glowView;
@@ -73,97 +75,16 @@ - (void)layoutSubviews {
- (void)setIsActive:(BOOL)isActive {
self.titleLabel.textColor = isActive ? [UIColor whiteColor] : [UIColor lightGrayColor];
if (isActive) {
- self.iconView.image = [self tabBarImage:self.image size:self.image.size backgroundImage:[UIImage imageNamed:@"selected-image-background.png"]];
+ self.iconView.image = self.selectedImage;
} else {
- self.iconView.image = [self tabBarImage:self.image size:self.image.size backgroundImage:nil];
+ self.iconView.image = self.unSelectedImage;
}
}
- (void)setIsGlowing:(BOOL)isGlowing {
self.glowView.hidden = !isGlowing;
}
--(UIImage*)tabBarImage:(UIImage*)startImage size:(CGSize)targetSize backgroundImage:(UIImage*)backgroundImageSource {
- UIImage* backgroundImage = [self tabBarBackgroundImageWithSize:startImage.size backgroundImage:backgroundImageSource];
-
- // Convert the passed in image to a white backround image with a black fill
- UIImage* bwImage = [self blackFilledImageWithWhiteBackgroundUsing:startImage];
-
- // Create an image mask
- CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(bwImage.CGImage),
- CGImageGetHeight(bwImage.CGImage),
- CGImageGetBitsPerComponent(bwImage.CGImage),
- CGImageGetBitsPerPixel(bwImage.CGImage),
- CGImageGetBytesPerRow(bwImage.CGImage),
- CGImageGetDataProvider(bwImage.CGImage), NULL, YES);
-
- // Using the mask create a new image
- CGImageRef tabBarImageRef = CGImageCreateWithMask(backgroundImage.CGImage, imageMask);
-
- UIImage* tabBarImage = [UIImage imageWithCGImage:tabBarImageRef scale:startImage.scale orientation:startImage.imageOrientation];
-
- // Cleanup
- CGImageRelease(imageMask);
- CGImageRelease(tabBarImageRef);
-
- // Create a new context with the right size
- UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0.0);
-
- // Draw the new tab bar image at the center
- [tabBarImage drawInRect:CGRectMake((targetSize.width/2.0) - (startImage.size.width/2.0), (targetSize.height/2.0) - (startImage.size.height/2.0), startImage.size.width, startImage.size.height)];
-
- // Generate a new image
- UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return resultImage;
-}
-
-// Convert the image's fill color to black and background to white
-- (UIImage *)blackFilledImageWithWhiteBackgroundUsing:(UIImage*)startImage {
- // Create the proper sized rect
- CGRect imageRect = CGRectMake(0, 0, CGImageGetWidth(startImage.CGImage), CGImageGetHeight(startImage.CGImage));
-
- // Create a new bitmap context
- CGContextRef context = CGBitmapContextCreate(NULL, imageRect.size.width, imageRect.size.height, 8, 0, CGImageGetColorSpace(startImage.CGImage), kCGImageAlphaPremultipliedLast);
-
- CGContextSetRGBFillColor(context, 1, 1, 1, 1);
- CGContextFillRect(context, imageRect);
-
- // Use the passed in image as a clipping mask
- CGContextClipToMask(context, imageRect, startImage.CGImage);
- // Set the fill color to black: R:0 G:0 B:0 alpha:1
- CGContextSetRGBFillColor(context, 0, 0, 0, 1);
- // Fill with black
- CGContextFillRect(context, imageRect);
-
- // Generate a new image
- CGImageRef newCGImage = CGBitmapContextCreateImage(context);
- UIImage* newImage = [UIImage imageWithCGImage:newCGImage scale:startImage.scale orientation:startImage.imageOrientation];
-
- // Cleanup
- CGContextRelease(context);
- CGImageRelease(newCGImage);
-
- return newImage;
-}
-
-- (UIImage *)tabBarBackgroundImageWithSize:(CGSize)targetSize backgroundImage:(UIImage*)backgroundImage {
- UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0.0);
- if (backgroundImage) {
- // Draw the background image centered
- [backgroundImage drawInRect:CGRectMake((targetSize.width - CGImageGetWidth(backgroundImage.CGImage)) / 2, (targetSize.height - CGImageGetHeight(backgroundImage.CGImage)) / 2, CGImageGetWidth(backgroundImage.CGImage), CGImageGetHeight(backgroundImage.CGImage))];
- } else {
- [[UIColor lightGrayColor] set];
- UIRectFill(CGRectMake(0, 0, targetSize.width, targetSize.height));
- }
-
- UIImage *finalBackgroundImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return finalBackgroundImage;
-}
-
@end
@interface CKSideBarController () <UITableViewDelegate, UITableViewDataSource>
@@ -220,6 +141,12 @@ - (BOOL)shouldAutorotate {
return YES;
}
+#pragma mark - public
+
+- (void)refresh {
+ [self.sideBarView reloadData];
+}
+
- (void)setViewControllers:(NSArray *)viewControllers {
if (viewControllers != _viewControllers) {
BOOL isSelectedControllerPresent = [viewControllers containsObject:_selectedViewController];
@@ -292,13 +219,13 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
if (!cell) cell = [[CKSideBarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
UIViewController *viewController = self.viewControllers[indexPath.row];
- NSString *title = viewController.tabBarItem.title ? viewController.tabBarItem.title : viewController.title;
- UIImage *image = viewController.tabBarItem.image ? viewController.tabBarItem.image : [UIImage imageNamed:@"default-tabbar-icon.png"];
+ CKSideBarItem *sideBarItem = viewController.sideBarItem;
- cell.titleLabel.text = title;
- [cell setImage:image];
+ cell.titleLabel.text = sideBarItem.title;
+ cell.selectedImage = sideBarItem.selectedImage;
+ cell.unSelectedImage = sideBarItem.unSelectedImage;
[cell setIsActive:(viewController == self.selectedViewController)];
- [cell setIsGlowing:[self.delegate respondsToSelector:@selector(sideBarController:rowShouldGlow:)] && [self.delegate sideBarController:self rowShouldGlow:indexPath.row]];
+ [cell setIsGlowing:sideBarItem.isGlowing];
return cell;
}
@@ -308,4 +235,37 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[self.sideBarView reloadData];
}
+@end
+
+@implementation UIViewController (CKSideBarController)
+
+- (CKSideBarController *)sideBarController {
+ UIViewController *parent = self.parentViewController;
+ while (parent != nil) {
+ if ([parent isKindOfClass:[CKSideBarController class]]) {
+ return (CKSideBarController *)parent;
+ } else {
+ parent = parent.parentViewController;
+ }
+ }
+ return nil;
+}
+
+static char const * const CKSideBarItemKey = "CKSideBarItemKey";
+
+- (void)setSideBarItem:(CKSideBarItem *)sideBarItem {
+ objc_setAssociatedObject(self, CKSideBarItemKey, sideBarItem, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+}
+
+- (CKSideBarItem *)sideBarItem {
+ CKSideBarItem *item = objc_getAssociatedObject(self, CKSideBarItemKey);
+ if (!item) {
+ item = [[CKSideBarItem alloc] init];
+ item.title = self.title;
+ item.image = [UIImage imageNamed:@"default-tabbar-icon.png"];
+ [self setSideBarItem:item];
+ }
+ return item;
+}
+
@end
View
@@ -0,0 +1,30 @@
+//
+// Copyright (c) 2012 Jason Kozemczak
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
+// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
+// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface CKSideBarItem : NSObject
+
+@property (nonatomic) NSString *title;
+@property (nonatomic) UIImage *image;
+@property (nonatomic) BOOL isGlowing;
+
+@property (nonatomic, readonly) UIImage *selectedImage;
+@property (nonatomic, readonly) UIImage *unSelectedImage;
+
+@end
Oops, something went wrong.

0 comments on commit c9347cd

Please sign in to comment.