Skip to content

Instantly share code, notes, and snippets.

@jchavarri
Last active December 14, 2015 20:49
Show Gist options
  • Save jchavarri/5146939 to your computer and use it in GitHub Desktop.
Save jchavarri/5146939 to your computer and use it in GitHub Desktop.
UIShadowButton - A button that moves and changes its shadow when it gets pressed.
//
// UIShadowButton.h
//
// Created by Javier Chavarri on 12/03/13.
//
#import <UIKit/UIKit.h>
@interface UIShadowButton : UIButton
@end
//
// UIShadowButton.m
//
// Created by Javier Chavarri on 12/03/13.
//
#import "UIShadowButton.h"
#import <QuartzCore/QuartzCore.h>
#import <Foundation/Foundation.h>
@implementation UIShadowButton
-(void)setupView{
self.layer.backgroundColor = [UIColor blueColor].CGColor;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.65;
self.layer.shadowRadius = 1.5f;
self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 1.0;
}
-(id)initWithFrame:(CGRect)frame{
if((self = [super initWithFrame:frame])){
[self setupView];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if((self = [super initWithCoder:aDecoder])){
[self setupView];
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGRect theFrame = self.frame;
theFrame.origin.x += 2;
theFrame.origin.y += 2;
self.frame = theFrame;
//self.contentEdgeInsets = UIEdgeInsetsMake(2.0,2.0,-2.0,-2.0);
self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
//self.layer.shadowOpacity = 0.8;
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CGRect theFrame = self.frame;
theFrame.origin.x -= 2;
theFrame.origin.y -= 2;
self.frame = theFrame;
//self.contentEdgeInsets = UIEdgeInsetsMake(0.0,0.0,0.0,0.0);
self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
//self.layer.shadowOpacity = 0.5;
[super touchesEnded:touches withEvent:event];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment