Skip to content

Instantly share code, notes, and snippets.

@slabko
Created December 28, 2014 20:24
Show Gist options
  • Save slabko/1e608c0db95d5808f5b4 to your computer and use it in GitHub Desktop.
Save slabko/1e608c0db95d5808f5b4 to your computer and use it in GitHub Desktop.
Square button with background animation on touch down and up (Highlight)
#import <UIKit/UIKit.h>
@interface ARLSquareButton : UIButton
@property (nonatomic, strong) IBInspectable UIColor *highlightedBackgroundColor;
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
@end
#import "ARLSquareButton.h"
IB_DESIGNABLE
@interface ARLSquareButton()
@property (nonatomic, strong) UIColor *normalBackgroundColor;
@end
@implementation ARLSquareButton
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
[self setup];
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup
{
self.highlightedBackgroundColor = [UIColor lightGrayColor];
[self addTarget:self action:@selector(processTouchDown:) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(processTouchDown:) forControlEvents:UIControlEventTouchDragEnter];
[self addTarget:self action:@selector(processTouchUp:) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(processTouchUp:) forControlEvents:UIControlEventTouchDragExit];
}
- (void)setCornerRadius:(CGFloat)cornerRadius
{
self.layer.cornerRadius = cornerRadius;
}
- (CGFloat)cornerRadius
{
return self.layer.cornerRadius;
}
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
[super setBackgroundColor:backgroundColor];
self.normalBackgroundColor = backgroundColor;
}
- (UIColor *)backgroundColor
{
return self.normalBackgroundColor;
}
- (void)processTouchDown:(id)sender
{
[super setBackgroundColor:self.highlightedBackgroundColor];
}
- (void)processTouchUp:(id)sender
{
super.backgroundColor = [self normalBackgroundColor];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment