From 304a9574ccd0a78decd55f17afb732d53fb60113 Mon Sep 17 00:00:00 2001 From: Suyeol Jeon Date: Mon, 1 Sep 2014 01:16:31 +0900 Subject: [PATCH] Add `cornerRadius` property. --- SSBouncyButton/SSBouncyButton.h | 2 + SSBouncyButton/SSBouncyButton.m | 45 ++++++++++++++----- .../SSBouncyButtonDemo/SSAppDelegate.m | 16 ++++--- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/SSBouncyButton/SSBouncyButton.h b/SSBouncyButton/SSBouncyButton.h index 6cd4c54..4f778e5 100644 --- a/SSBouncyButton/SSBouncyButton.h +++ b/SSBouncyButton/SSBouncyButton.h @@ -32,4 +32,6 @@ static CGFloat SSBouncyButtonDefaultCornerRadius = 3; @interface SSBouncyButton : UIButton +@property (nonatomic, assign) CGFloat cornerRadius; + @end diff --git a/SSBouncyButton/SSBouncyButton.m b/SSBouncyButton/SSBouncyButton.m index eda1d61..01005ce 100644 --- a/SSBouncyButton/SSBouncyButton.m +++ b/SSBouncyButton/SSBouncyButton.m @@ -51,6 +51,7 @@ - (id)initWithFrame:(CGRect)frame if (self) { self.adjustsImageWhenHighlighted = NO; self.tintColor = [UIColor colorWithHex:SSBouncyButtonDefaultTintColor]; + self.cornerRadius = SSBouncyButtonDefaultCornerRadius; self.titleLabel.font = [UIFont systemFontOfSize:SSBouncyButtonDefaultTitleLabelFontSize]; [self setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; [self setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected | UIControlStateHighlighted]; @@ -58,32 +59,51 @@ - (id)initWithFrame:(CGRect)frame return self; } + +#pragma mark - Properties + - (void)setTintColor:(UIColor *)tintColor { [super setTintColor:tintColor]; [self setTitleColor:tintColor forState:UIControlStateNormal]; - - NSDictionary *borderAttrs = @{NSStrokeColorAttributeName: tintColor, + [self updateBackgroundImage]; +} + +- (void)setCornerRadius:(CGFloat)cornerRadius +{ + _cornerRadius = cornerRadius; + [self updateBackgroundImage]; +} + +- (void)setTitle:(NSString *)title forState:(UIControlState)state +{ + [super setTitle:title forState:state]; + if (state == UIControlStateSelected) { + [self setTitle:title forState:UIControlStateSelected | UIControlStateHighlighted]; + } +} + + +#pragma mark - Draw + +- (void)updateBackgroundImage +{ + NSDictionary *borderAttrs = @{NSStrokeColorAttributeName: self.tintColor, NSStrokeWidthAttributeName: @(SSBouncyButtonDefaultBorderWidth)}; UIImage *normalBackgroundImage = [UIImage resizableImageWithColor:[UIColor whiteColor] borderAttributes:borderAttrs - cornerRadius:SSBouncyButtonDefaultCornerRadius]; + cornerRadius:self.cornerRadius]; UIImage *selectedBackgroundImage = [UIImage resizableImageWithColor:self.tintColor borderAttributes:borderAttrs - cornerRadius:SSBouncyButtonDefaultCornerRadius]; + cornerRadius:self.cornerRadius]; [self setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal]; [self setBackgroundImage:selectedBackgroundImage forState:UIControlStateSelected]; [self setBackgroundImage:selectedBackgroundImage forState:UIControlStateSelected | UIControlStateHighlighted]; } -- (void)setTitle:(NSString *)title forState:(UIControlState)state -{ - [super setTitle:title forState:state]; - if (state == UIControlStateSelected) { - [self setTitle:title forState:UIControlStateSelected | UIControlStateHighlighted]; - } -} + +#pragma mark - Touch Event - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { @@ -116,6 +136,9 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event [self beginEnlargeAnimation]; } + +#pragma mark - Animations + - (void)beginShrinkAnimation { [self.touchDelayTimer invalidate]; diff --git a/SSBouncyButtonDemo/SSBouncyButtonDemo/SSAppDelegate.m b/SSBouncyButtonDemo/SSBouncyButtonDemo/SSAppDelegate.m index 177ab59..d11c99c 100644 --- a/SSBouncyButtonDemo/SSBouncyButtonDemo/SSAppDelegate.m +++ b/SSBouncyButtonDemo/SSBouncyButtonDemo/SSAppDelegate.m @@ -48,13 +48,15 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [followButton addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:followButton]; - SSBouncyButton *tintedButton = [[SSBouncyButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)]; - tintedButton.tintColor = [UIColor grayColor]; - tintedButton.center = CGPointMake(centerX, centerY + 25); - [tintedButton setTitle:@"Follow" forState:UIControlStateNormal]; - [tintedButton setTitle:@"Following" forState:UIControlStateSelected]; - [tintedButton addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside]; - [self.window addSubview:tintedButton]; + // tintColor, cornerRadius + SSBouncyButton *customButton = [[SSBouncyButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)]; + customButton.center = CGPointMake(centerX, centerY + 25); + customButton.tintColor = [UIColor grayColor]; + customButton.cornerRadius = 5; + [customButton setTitle:@"Follow" forState:UIControlStateNormal]; + [customButton setTitle:@"Following" forState:UIControlStateSelected]; + [customButton addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside]; + [self.window addSubview:customButton]; return YES; }