Skip to content

Commit

Permalink
Add cornerRadius property.
Browse files Browse the repository at this point in the history
  • Loading branch information
devxoul committed Aug 31, 2014
1 parent db8dec0 commit 304a957
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
2 changes: 2 additions & 0 deletions SSBouncyButton/SSBouncyButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ static CGFloat SSBouncyButtonDefaultCornerRadius = 3;

@interface SSBouncyButton : UIButton

@property (nonatomic, assign) CGFloat cornerRadius;

@end
45 changes: 34 additions & 11 deletions SSBouncyButton/SSBouncyButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,59 @@ - (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];
}
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
{
Expand Down Expand Up @@ -116,6 +136,9 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
[self beginEnlargeAnimation];
}


#pragma mark - Animations

- (void)beginShrinkAnimation
{
[self.touchDelayTimer invalidate];
Expand Down
16 changes: 9 additions & 7 deletions SSBouncyButtonDemo/SSBouncyButtonDemo/SSAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 304a957

Please sign in to comment.