diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93a77f2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ + +.DS_Store + +YRDropdownExample.xcodeproj/project.xcworkspace/xcuserdata/quadrani.xcuserdatad/UserInterfaceState.xcuserstate + +YRDropdownExample.xcodeproj/xcuserdata/quadrani.xcuserdatad/xcschemes/YRDropdownExample.xcscheme + +YRDropdownExample.xcodeproj/xcuserdata/quadrani.xcuserdatad/xcschemes/xcschememanagement.plist diff --git a/YRDropdownExample.xcodeproj/project.pbxproj b/YRDropdownExample.xcodeproj/project.pbxproj index 03e491c..da6bf9c 100644 --- a/YRDropdownExample.xcodeproj/project.pbxproj +++ b/YRDropdownExample.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 01B3592615165DCA001F266C /* YRDropdownView.m in Sources */ = {isa = PBXBuildFile; fileRef = 01B3592315165DCA001F266C /* YRDropdownView.m */; }; 2D3DC25114D73F8E00898E62 /* dropdown-alert@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 2D3DC24F14D73F8E00898E62 /* dropdown-alert@2x.png */; }; 2D3DC25214D73F8E00898E62 /* dropdown-alert.png in Resources */ = {isa = PBXBuildFile; fileRef = 2D3DC25014D73F8E00898E62 /* dropdown-alert.png */; }; 2D5FCB4D14D30C2C00DA334B /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2D5FCB4C14D30C2C00DA334B /* UIKit.framework */; }; @@ -17,12 +18,11 @@ 2D5FCB5D14D30C2C00DA334B /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D5FCB5C14D30C2C00DA334B /* AppDelegate.m */; }; 2D5FCB6014D30C2C00DA334B /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D5FCB5F14D30C2C00DA334B /* ViewController.m */; }; 2D5FCB6314D30C2C00DA334B /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2D5FCB6114D30C2C00DA334B /* ViewController.xib */; }; - 2DC084D814D878460013863F /* bg-yellow.png in Resources */ = {isa = PBXBuildFile; fileRef = 2DC084D414D878460013863F /* bg-yellow.png */; }; - 2DC084D914D878460013863F /* bg-yellow@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 2DC084D514D878460013863F /* bg-yellow@2x.png */; }; - 2DC084DA14D878460013863F /* YRDropdownView.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DC084D714D878460013863F /* YRDropdownView.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 01B3592215165DCA001F266C /* YRDropdownView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YRDropdownView.h; sourceTree = ""; }; + 01B3592315165DCA001F266C /* YRDropdownView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YRDropdownView.m; sourceTree = ""; }; 2D3DC24F14D73F8E00898E62 /* dropdown-alert@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "dropdown-alert@2x.png"; sourceTree = ""; }; 2D3DC25014D73F8E00898E62 /* dropdown-alert.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "dropdown-alert.png"; sourceTree = ""; }; 2D5FCB4814D30C2B00DA334B /* YRDropdownExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = YRDropdownExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -39,10 +39,6 @@ 2D5FCB5F14D30C2C00DA334B /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 2D5FCB6214D30C2C00DA334B /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController.xib; sourceTree = ""; }; 2D5FCB7214D321BC00DA334B /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; - 2DC084D414D878460013863F /* bg-yellow.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "bg-yellow.png"; path = "YRDropdownView/bg-yellow.png"; sourceTree = ""; }; - 2DC084D514D878460013863F /* bg-yellow@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "bg-yellow@2x.png"; path = "YRDropdownView/bg-yellow@2x.png"; sourceTree = ""; }; - 2DC084D614D878460013863F /* YRDropdownView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = YRDropdownView.h; path = YRDropdownView/YRDropdownView.h; sourceTree = ""; }; - 2DC084D714D878460013863F /* YRDropdownView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = YRDropdownView.m; path = YRDropdownView/YRDropdownView.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -59,16 +55,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ - 2D3DC25414D7419F00898E62 /* YRDropdownView */ = { + 01B3591F15165DCA001F266C /* YRDropdownView */ = { isa = PBXGroup; children = ( - 2DC084D414D878460013863F /* bg-yellow.png */, - 2DC084D514D878460013863F /* bg-yellow@2x.png */, - 2DC084D614D878460013863F /* YRDropdownView.h */, - 2DC084D714D878460013863F /* YRDropdownView.m */, + 01B3592215165DCA001F266C /* YRDropdownView.h */, + 01B3592315165DCA001F266C /* YRDropdownView.m */, ); - name = YRDropdownView; - sourceTree = ""; + path = YRDropdownView; + sourceTree = SOURCE_ROOT; }; 2D5FCB3D14D30C2B00DA334B = { isa = PBXGroup; @@ -104,8 +98,8 @@ 2D5FCB5B14D30C2C00DA334B /* AppDelegate.h */, 2D5FCB5C14D30C2C00DA334B /* AppDelegate.m */, 2DC084D314D878180013863F /* View Controller */, + 01B3591F15165DCA001F266C /* YRDropdownView */, 2D5FCB5314D30C2C00DA334B /* Supporting Files */, - 2D3DC25414D7419F00898E62 /* YRDropdownView */, 2D3DC24F14D73F8E00898E62 /* dropdown-alert@2x.png */, 2D3DC25014D73F8E00898E62 /* dropdown-alert.png */, ); @@ -188,8 +182,6 @@ 2D5FCB6314D30C2C00DA334B /* ViewController.xib in Resources */, 2D3DC25114D73F8E00898E62 /* dropdown-alert@2x.png in Resources */, 2D3DC25214D73F8E00898E62 /* dropdown-alert.png in Resources */, - 2DC084D814D878460013863F /* bg-yellow.png in Resources */, - 2DC084D914D878460013863F /* bg-yellow@2x.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -203,7 +195,7 @@ 2D5FCB5914D30C2C00DA334B /* main.m in Sources */, 2D5FCB5D14D30C2C00DA334B /* AppDelegate.m in Sources */, 2D5FCB6014D30C2C00DA334B /* ViewController.m in Sources */, - 2DC084DA14D878460013863F /* YRDropdownView.m in Sources */, + 01B3592615165DCA001F266C /* YRDropdownView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/YRDropdownExample/YRDropdownView/YRDropdownView.h b/YRDropdownExample/YRDropdownView/YRDropdownView.h deleted file mode 100644 index 8683615..0000000 --- a/YRDropdownExample/YRDropdownView/YRDropdownView.h +++ /dev/null @@ -1,69 +0,0 @@ -// -// YRDropdownView.h -// YRDropdownViewExample -// -// Created by Eli Perkins on 1/27/12. -// Copyright (c) 2012 One Mighty Roar. All rights reserved. -// - -#import -#import - -@interface YRDropdownView : UIView -{ - NSString *titleText; - NSString *detailText; - UILabel *titleLabel; - UILabel *detailLabel; - UIImage *backgroundImage; - UIImageView *backgroundImageView; - UIImage *accessoryImage; - UIImageView *accessoryImageView; - SEL onTouch; - NSDate *showStarted; - BOOL shouldAnimate; -} - -@property (copy) NSString *titleText; -@property (copy) NSString *detailText; -@property (assign) UIImage *accessoryImage; -@property (assign) float minHeight; -@property (nonatomic, assign) UIImage *backgroundImage; -@property (nonatomic, assign) SEL onTouch; -@property (assign) BOOL shouldAnimate; - -#pragma mark - View methods - -+ (YRDropdownView *)showDropdownInView:(UIView *)view - title:(NSString *)title; - -+ (YRDropdownView *)showDropdownInView:(UIView *)view - title:(NSString *)title - detail:(NSString *)detail; - -+ (YRDropdownView *)showDropdownInView:(UIView *)view - title:(NSString *)title - detail:(NSString *)detail - animated:(BOOL)animated; - -+ (YRDropdownView *)showDropdownInView:(UIView *)view - title:(NSString *)title - detail:(NSString *)detail - image:(UIImage *)image - animated:(BOOL)animated; - -+ (YRDropdownView *)showDropdownInView:(UIView *)view - title:(NSString *)title - detail:(NSString *)detail - image:(UIImage *)image - animated:(BOOL)animated - hideAfter:(float)delay; - -+ (BOOL)hideDropdownInView:(UIView *)view; -+ (BOOL)hideDropdownInView:(UIView *)view animated:(BOOL)animated; - -#pragma mark - -- (void)show:(BOOL)animated; -- (void)hide:(BOOL)animated; - -@end diff --git a/YRDropdownExample/YRDropdownView/YRDropdownView.m b/YRDropdownExample/YRDropdownView/YRDropdownView.m deleted file mode 100644 index ccbd655..0000000 --- a/YRDropdownExample/YRDropdownView/YRDropdownView.m +++ /dev/null @@ -1,406 +0,0 @@ -// -// YRDropdownView.m -// YRDropdownViewExample -// -// Created by Eli Perkins on 1/27/12. -// Copyright (c) 2012 One Mighty Roar. All rights reserved. -// - -#import "YRDropdownView.h" -#import - -@interface UILabel (YRDropdownView) -- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth; -@end - -@implementation UILabel (YRDropdownView) - - -- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth -{ - self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0); - self.lineBreakMode = UILineBreakModeWordWrap; - self.numberOfLines = 0; - [self sizeToFit]; -} -@end - -@interface YRDropdownView () -- (void)updateTitleLabel:(NSString *)newText; -- (void)updateDetailLabel:(NSString *)newText; -- (void)hideUsingAnimation:(NSNumber *)animated; -- (void)done; -@end - - -@implementation YRDropdownView -@synthesize titleText; -@synthesize detailText; -@synthesize minHeight; -@synthesize backgroundImage; -@synthesize accessoryImage; -@synthesize onTouch; -@synthesize shouldAnimate; - -//Using this prevents two alerts to ever appear on the screen at the same time -//TODO: Queue alerts, if multiple -static YRDropdownView *currentDropdown = nil; - -+ (YRDropdownView *)currentDropdownView -{ - return currentDropdown; -} - -#pragma mark - Accessors - -- (NSString *)titleText -{ - return titleText; -} - -- (void)setTitleText:(NSString *)newText -{ - if ([NSThread isMainThread]) { - [self updateTitleLabel:newText]; - [self setNeedsLayout]; - [self setNeedsDisplay]; - } else { - [self performSelectorOnMainThread:@selector(updateTitleLabel:) withObject:newText waitUntilDone:NO]; - [self performSelectorOnMainThread:@selector(setNeedsLayout) withObject:nil waitUntilDone:NO]; - [self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO]; - } -} - -- (NSString *)detailText -{ - return detailText; -} - -- (void)setDetailText:(NSString *)newText -{ - if ([NSThread isMainThread]) { - [self updateDetailLabel:newText]; - [self setNeedsLayout]; - [self setNeedsDisplay]; - } else { - [self performSelectorOnMainThread:@selector(updateDetailLabel:) withObject:newText waitUntilDone:NO]; - [self performSelectorOnMainThread:@selector(setNeedsLayout) withObject:nil waitUntilDone:NO]; - [self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO]; - } -} - -- (void)updateTitleLabel:(NSString *)newText { - if (titleText != newText) { - #if !__has_feature(objc_arc) - [titleText release]; - #endif - titleText = [newText copy]; - titleLabel.text = titleText; - } -} - -- (void)updateDetailLabel:(NSString *)newText { - if (detailText != newText) { - #if !__has_feature(objc_arc) - [detailText release]; - #endif - detailText = [newText copy]; - detailLabel.text = detailText; - } -} - - - -#pragma mark - Initializers -- (id)init { - return [self initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)]; -} - -- (id)initWithFrame:(CGRect)frame -{ - self = [super initWithFrame:frame]; - if (self) { - // Initialization code - self.titleText = nil; - self.detailText = nil; - self.minHeight = 44.0f; - self.backgroundImage = [UIImage imageNamed:@"bg-yellow.png"]; - self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; - - titleLabel = [[UILabel alloc] initWithFrame:self.bounds]; - detailLabel = [[UILabel alloc] initWithFrame:self.bounds]; - backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds]; - backgroundImageView.image = [self.backgroundImage stretchableImageWithLeftCapWidth:1 topCapHeight:self.backgroundImage.size.height/2]; - backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth; - - accessoryImageView = [[UIImageView alloc] initWithFrame:self.bounds]; - [self addSubview:backgroundImageView]; - - self.opaque = YES; - - onTouch = @selector(hide:); - } - return self; -} - -#pragma mark - Defines - -#define HORIZONTAL_PADDING 15.0f -#define VERTICAL_PADDING 19.0f -#define IMAGE_PADDING 45.0f -#define TITLE_FONT_SIZE 19.0f -#define DETAIL_FONT_SIZE 13.0f -#define ANIMATION_DURATION 0.3f - -#pragma mark - Class methods -#pragma mark View Methods -+ (YRDropdownView *)showDropdownInView:(UIView *)view title:(NSString *)title -{ - return [YRDropdownView showDropdownInView:view title:title detail:nil]; -} - -+ (YRDropdownView *)showDropdownInView:(UIView *)view title:(NSString *)title detail:(NSString *)detail -{ - return [YRDropdownView showDropdownInView:view title:title detail:detail image:nil animated:YES]; -} - -+ (YRDropdownView *)showDropdownInView:(UIView *)view title:(NSString *)title detail:(NSString *)detail animated:(BOOL)animated -{ - return [YRDropdownView showDropdownInView:view title:title detail:detail image:nil animated:animated hideAfter:0.0]; -} - -+ (YRDropdownView *)showDropdownInView:(UIView *)view title:(NSString *)title detail:(NSString *)detail image:(UIImage *)image animated:(BOOL)animated -{ - return [YRDropdownView showDropdownInView:view title:title detail:detail image:image animated:animated hideAfter:0.0]; -} - -+ (YRDropdownView *)showDropdownInView:(UIView *)view - title:(NSString *)title - detail:(NSString *)detail - image:(UIImage *)image - animated:(BOOL)animated - hideAfter:(float)delay -{ - if (currentDropdown) { - [currentDropdown hideUsingAnimation:[NSNumber numberWithBool:animated]]; - } - - YRDropdownView *dropdown = [[YRDropdownView alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, 44)]; - currentDropdown = dropdown; - dropdown.titleText = title; - - if (detail) { - dropdown.detailText = detail; - } - - if (image) { - dropdown.accessoryImage = image; - } - - dropdown.shouldAnimate = animated; - - if ([view isKindOfClass:[UIWindow class]]) { - CGRect dropdownFrame = dropdown.frame; - CGRect appFrame = [[UIScreen mainScreen] applicationFrame]; - dropdownFrame.origin.y = appFrame.origin.y; - dropdown.frame = dropdownFrame; - } - - [view addSubview:dropdown]; - [dropdown show:animated]; - if (delay != 0.0) { - [dropdown performSelector:@selector(hideUsingAnimation:) withObject:[NSNumber numberWithBool:animated] afterDelay:delay+ANIMATION_DURATION]; - } - - return dropdown; -} - -+ (void)removeView -{ - if (!currentDropdown) { - return; - } - - [currentDropdown removeFromSuperview]; - - [currentDropdown release]; - currentDropdown = nil; -} - -+ (BOOL)hideDropdownInView:(UIView *)view -{ - return [YRDropdownView hideDropdownInView:view animated:YES]; -} - -+ (BOOL)hideDropdownInView:(UIView *)view animated:(BOOL)animated -{ - if (currentDropdown) { - [currentDropdown hideUsingAnimation:[NSNumber numberWithBool:animated]]; - return YES; - } - - UIView *viewToRemove = nil; - for (UIView *v in [view subviews]) { - if ([v isKindOfClass:[YRDropdownView class]]) { - viewToRemove = v; - } - } - if (viewToRemove != nil) { - YRDropdownView *dropdown = (YRDropdownView *)viewToRemove; - [dropdown hideUsingAnimation:[NSNumber numberWithBool:animated]]; - return YES; - } - else { - return NO; - } -} - -#pragma mark - Methods - -- (void)show:(BOOL)animated -{ - if(animated) - { - self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y-self.frame.size.height, self.frame.size.width, self.frame.size.height); - self.alpha = 0.02; - [UIView animateWithDuration:ANIMATION_DURATION - delay:0.0 - options:UIViewAnimationOptionCurveEaseInOut - animations:^{ - self.alpha = 1.0; - self.frame = CGRectMake(self.frame.origin.x, - self.frame.origin.y+self.frame.size.height, - self.frame.size.width, self.frame.size.height); - } - completion:^(BOOL finished) { - if (finished) - { - - } - }]; - - } -} - -- (void)hide:(BOOL)animated -{ - [self done]; -} - -- (void)hideUsingAnimation:(NSNumber *)animated { - if ([animated boolValue]) { - [UIView animateWithDuration:ANIMATION_DURATION - delay:0.0 - options:UIViewAnimationOptionCurveEaseInOut - animations:^{ - self.alpha = 0.02; - self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y-self.frame.size.height, self.frame.size.width, self.frame.size.height); - } - completion:^(BOOL finished) { - if (finished) - { - [self done]; - } - }]; - } - else { - self.alpha = 0.0f; - [self done]; - } -} - -- (void)done -{ - [self removeFromSuperview]; -} - -- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event -{ - [self hideUsingAnimation:[NSNumber numberWithBool:self.shouldAnimate]]; -} - -#pragma mark - Layout - -- (void)layoutSubviews { - // Set label properties - titleLabel.font = [UIFont boldSystemFontOfSize:TITLE_FONT_SIZE]; - titleLabel.adjustsFontSizeToFitWidth = NO; - titleLabel.opaque = NO; - titleLabel.backgroundColor = [UIColor clearColor]; - titleLabel.textColor = [UIColor colorWithWhite:0.225 alpha:1.0]; - titleLabel.shadowOffset = CGSizeMake(0, 1/[[UIScreen mainScreen] scale]); - titleLabel.shadowColor = [UIColor colorWithWhite:1 alpha:0.25]; - titleLabel.text = self.titleText; - [titleLabel sizeToFitFixedWidth:self.bounds.size.width - (2 * HORIZONTAL_PADDING)]; - - titleLabel.frame = CGRectMake(self.bounds.origin.x + HORIZONTAL_PADDING, - self.bounds.origin.y + VERTICAL_PADDING - 8, - self.bounds.size.width - (2 * HORIZONTAL_PADDING), - titleLabel.frame.size.height); - - [self addSubview:titleLabel]; - - if (self.detailText) { - detailLabel.font = [UIFont systemFontOfSize:DETAIL_FONT_SIZE]; - detailLabel.numberOfLines = 0; - detailLabel.adjustsFontSizeToFitWidth = NO; - detailLabel.opaque = NO; - detailLabel.backgroundColor = [UIColor clearColor]; - detailLabel.textColor = [UIColor colorWithWhite:0.225 alpha:1.0]; - detailLabel.shadowOffset = CGSizeMake(0, 1/[[UIScreen mainScreen] scale]); - detailLabel.shadowColor = [UIColor colorWithWhite:1 alpha:0.25]; - detailLabel.text = self.detailText; - [detailLabel sizeToFitFixedWidth:self.bounds.size.width - (2 * HORIZONTAL_PADDING)]; - - detailLabel.frame = CGRectMake(self.bounds.origin.x + HORIZONTAL_PADDING, - titleLabel.frame.origin.y + titleLabel.frame.size.height + 2, - self.bounds.size.width - (2 * HORIZONTAL_PADDING), - detailLabel.frame.size.height); - - [self addSubview:detailLabel]; - } else { - titleLabel.frame = CGRectMake(titleLabel.frame.origin.x, - 9, - titleLabel.frame.size.width, - titleLabel.frame.size.height); - } - - if (self.accessoryImage) { - accessoryImageView.image = self.accessoryImage; - accessoryImageView.frame = CGRectMake(self.bounds.origin.x + HORIZONTAL_PADDING, - self.bounds.origin.y + VERTICAL_PADDING, - self.accessoryImage.size.width, - self.accessoryImage.size.height); - - [titleLabel sizeToFitFixedWidth:self.bounds.size.width - IMAGE_PADDING - (HORIZONTAL_PADDING * 2)]; - titleLabel.frame = CGRectMake(titleLabel.frame.origin.x + IMAGE_PADDING, - titleLabel.frame.origin.y, - titleLabel.frame.size.width, - titleLabel.frame.size.height); - - if (self.detailText) { - [detailLabel sizeToFitFixedWidth:self.bounds.size.width - IMAGE_PADDING - (HORIZONTAL_PADDING * 2)]; - detailLabel.frame = CGRectMake(detailLabel.frame.origin.x + IMAGE_PADDING, - detailLabel.frame.origin.y, - detailLabel.frame.size.width, - detailLabel.frame.size.height); - } - - [self addSubview:accessoryImageView]; - } - - CGFloat dropdownHeight = 44.0f; - if (self.detailText) { - dropdownHeight = MAX(CGRectGetMaxY(self.bounds), CGRectGetMaxY(detailLabel.frame)); - dropdownHeight += VERTICAL_PADDING; - } - - [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, dropdownHeight)]; - - [backgroundImageView setFrame:self.bounds]; - -} - -@end - - diff --git a/YRDropdownExample/YRDropdownView/bg-yellow.png b/YRDropdownExample/YRDropdownView/bg-yellow.png deleted file mode 100644 index ace99d8..0000000 Binary files a/YRDropdownExample/YRDropdownView/bg-yellow.png and /dev/null differ diff --git a/YRDropdownExample/YRDropdownView/bg-yellow@2x.png b/YRDropdownExample/YRDropdownView/bg-yellow@2x.png deleted file mode 100644 index a76ba4d..0000000 Binary files a/YRDropdownExample/YRDropdownView/bg-yellow@2x.png and /dev/null differ diff --git a/YRDropdownExample/en.lproj/ViewController.xib b/YRDropdownExample/en.lproj/ViewController.xib index 81ad991..641cf3d 100644 --- a/YRDropdownExample/en.lproj/ViewController.xib +++ b/YRDropdownExample/en.lproj/ViewController.xib @@ -1,14 +1,14 @@ - 1280 - 11C74 - 2166 - 1138.23 - 567.00 + 1296 + 11D50b + 2182 + 1138.32 + 568.00 com.apple.InterfaceBuilder.IBCocoaTouchPlugin - 1166 + 1179 IBProxyObject @@ -37,7 +37,7 @@ - 292 + 301 {{124, 300}, {72, 37}} @@ -73,7 +73,7 @@ - 292 + 306 {{0, 81}, {320, 194}} @@ -90,7 +90,7 @@ - 292 + 301 {{119, 352}, {82, 37}} @@ -113,7 +113,7 @@ - 292 + 269 {{124, 405}, {72, 37}} @@ -308,8 +308,12 @@ 0 IBCocoaTouchFramework + + com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS + + YES 3 - 1166 + 1179 diff --git a/YRDropdownView/YRDropdownView.h b/YRDropdownView/YRDropdownView.h index 6844528..3117c99 100644 --- a/YRDropdownView/YRDropdownView.h +++ b/YRDropdownView/YRDropdownView.h @@ -24,6 +24,9 @@ SEL onTouch; NSDate *showStarted; BOOL shouldAnimate; + + @private + CGGradientRef _backgroundGradient; } @property (copy) NSString *titleText; diff --git a/YRDropdownView/YRDropdownView.m b/YRDropdownView/YRDropdownView.m index 7c084ba..f4e89ad 100644 --- a/YRDropdownView/YRDropdownView.m +++ b/YRDropdownView/YRDropdownView.m @@ -10,7 +10,9 @@ #import @interface UILabel (YRDropdownView) + - (void)sizeToFitFixedWidth:(CGFloat)fixedWidth; + @end @implementation UILabel (YRDropdownView) @@ -23,13 +25,18 @@ - (void)sizeToFitFixedWidth:(CGFloat)fixedWidth self.numberOfLines = 0; [self sizeToFit]; } + @end @interface YRDropdownView () + +- (CGGradientRef)backgroundGradient; + - (void)updateTitleLabel:(NSString *)newText; - (void)updateDetailLabel:(NSString *)newText; - (void)hideUsingAnimation:(NSNumber *)animated; - (void)done; + @end @@ -114,6 +121,7 @@ - (void)updateDetailLabel:(NSString *)newText { #pragma mark - Initializers + - (id)init { return [self initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)]; } @@ -123,16 +131,15 @@ - (id)initWithFrame:(CGRect)frame self = [super initWithFrame:frame]; if (self) { // Initialization code + _backgroundGradient = NULL; self.titleText = nil; self.detailText = nil; self.minHeight = 44.0f; -// self.backgroundImage = [UIImage imageNamed:@"bg-yellow.png"]; self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; titleLabel = [[UILabel alloc] initWithFrame:self.bounds]; detailLabel = [[UILabel alloc] initWithFrame:self.bounds]; backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds]; - backgroundImageView.image = [self.backgroundImage stretchableImageWithLeftCapWidth:1 topCapHeight:self.backgroundImage.size.height/2]; backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth; accessoryImageView = [[UIImageView alloc] initWithFrame:self.bounds]; @@ -145,6 +152,16 @@ - (id)initWithFrame:(CGRect)frame return self; } +#pragma mark - Memory Management + +- (void)dealloc +{ + if (_backgroundGradient) { + CGGradientRelease(_backgroundGradient); + } + [super dealloc]; +} + #pragma mark - Defines #define HORIZONTAL_PADDING 15.0f @@ -188,7 +205,7 @@ + (YRDropdownView *)showDropdownInView:(UIView *)view title:title detail:detail image:image - backgroundImage:[UIImage imageNamed:@"bg-yellow.png"] + backgroundImage:nil animated:animated hideAfter:delay]; } @@ -229,6 +246,7 @@ + (YRDropdownView *)showDropdownInView:(UIView *)view YRDropdownView *dropdown = [[YRDropdownView alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, 44)]; currentDropdown = dropdown; dropdown.titleText = title; + dropdown.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; if (detail) { dropdown.detailText = detail; @@ -240,8 +258,6 @@ + (YRDropdownView *)showDropdownInView:(UIView *)view if (backgroundImage) { dropdown.backgroundImage = backgroundImage; - } else { - dropdown.backgroundImage = [UIImage imageNamed:@"bg-yellow.png"]; } if (titleLabelColor) { @@ -263,7 +279,7 @@ + (YRDropdownView *)showDropdownInView:(UIView *)view [view addSubview:dropdown]; [dropdown show:animated]; - if (delay != 0.0) { + if (delay > 0.0) { [dropdown performSelector:@selector(hideUsingAnimation:) withObject:[NSNumber numberWithBool:animated] afterDelay:delay+ANIMATION_DURATION]; } @@ -305,8 +321,7 @@ + (BOOL)hideDropdownInView:(UIView *)view animated:(BOOL)animated YRDropdownView *dropdown = (YRDropdownView *)viewToRemove; [dropdown hideUsingAnimation:[NSNumber numberWithBool:animated]]; return YES; - } - else { + } else { return NO; } } @@ -358,8 +373,7 @@ - (void)hideUsingAnimation:(NSNumber *)animated { [self done]; } }]; - } - else { + } else { self.alpha = 0.0f; [self done]; } @@ -388,13 +402,13 @@ - (void)layoutSubviews { CGColorRef color = [titleLabel.textColor CGColor]; const CGFloat *components = CGColorGetComponents(color); float total = 0.0; - for (int i = 0; i < (int)CGColorGetNumberOfComponents(color)-1; i++){ + for (int i = 0; i < (int)CGColorGetNumberOfComponents(color)-1; i++) { total += components[i]; } if ((total/((int)CGColorGetNumberOfComponents(color)-1)) > 0.5){ titleLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.25]; titleLabel.shadowOffset = CGSizeMake(0, -1/[[UIScreen mainScreen] scale]); - }else { + } else { titleLabel.shadowColor = [UIColor colorWithWhite:1 alpha:0.35]; titleLabel.shadowOffset = CGSizeMake(0, 1/[[UIScreen mainScreen] scale]); } @@ -426,7 +440,7 @@ - (void)layoutSubviews { if ((total/((int)CGColorGetNumberOfComponents(color)-1)) > 0.5){ detailLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.25]; detailLabel.shadowOffset = CGSizeMake(0, -1/[[UIScreen mainScreen] scale]); - }else { + } else { detailLabel.shadowColor = [UIColor colorWithWhite:1 alpha:0.35]; detailLabel.shadowOffset = CGSizeMake(0, 1/[[UIScreen mainScreen] scale]); } @@ -484,11 +498,35 @@ - (void)layoutSubviews { [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, dropdownHeight)]; - [backgroundImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:1 topCapHeight:backgroundImage.size.height/2]]; - [backgroundImageView setFrame:self.bounds]; - + if (self.backgroundImage) { + backgroundImageView.image = [self.backgroundImage stretchableImageWithLeftCapWidth:1 topCapHeight:self.backgroundImage.size.height/2]; + backgroundImageView.frame = self.bounds; + } } -@end +#pragma mark - Draw Methods + +- (CGGradientRef)backgroundGradient { + if (NULL == _backgroundGradient) { + CGFloat colors[16] = {255.0 / 255.0, 228.0 / 255.0, 137.0 / 255.0, 1.0, + 242.0 / 255.0, 203.0 / 255.0, 71.0 / 255.0, 1.0, + 241.0 / 255.0, 211.0 / 255.0, 108.0 / 255.0, 1.0, + 197.0 / 255.0, 157.0 / 255.0, 19.0 / 255.0, 1.0 + }; + CGFloat colorStops[4] = {0.0, 0.85, 0.95, 1.0}; + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + _backgroundGradient = CGGradientCreateWithColorComponents(colorSpace, colors, colorStops, 4); + CGColorSpaceRelease(colorSpace); + } + return _backgroundGradient; +} +- (void)drawRect:(CGRect)rect { + CGContextRef ctx = UIGraphicsGetCurrentContext(); + + CGPoint startPoint = {0.0, 0.0}; + CGPoint endPoint = {0.0, self.bounds.size.height}; + CGContextDrawLinearGradient(ctx, [self backgroundGradient], startPoint, endPoint, 0); +} +@end diff --git a/YRDropdownView/bg-yellow.png b/YRDropdownView/bg-yellow.png deleted file mode 100644 index ace99d8..0000000 Binary files a/YRDropdownView/bg-yellow.png and /dev/null differ diff --git a/YRDropdownView/bg-yellow@2x.png b/YRDropdownView/bg-yellow@2x.png deleted file mode 100644 index a76ba4d..0000000 Binary files a/YRDropdownView/bg-yellow@2x.png and /dev/null differ