Skip to content

Instantly share code, notes, and snippets.

@mundstein
Created June 23, 2012 11:54
Show Gist options
  • Save mundstein/2978020 to your computer and use it in GitHub Desktop.
Save mundstein/2978020 to your computer and use it in GitHub Desktop.
Button with drop shadow for iPhone which also animates down when pressed.
//
// ShadowButton.h
#import <Foundation/Foundation.h>
@interface ShadowButton : UIButton {
}
@end
//
// ShadowButton.m
#import "ShadowButton.h"
#import <QuartzCore/QuartzCore.h>
@implementation ShadowButton
-(void)setupView{
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 1;
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
}
-(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{
self.contentEdgeInsets = UIEdgeInsetsMake(1.0,1.0,-1.0,-1.0);
self.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
self.layer.shadowOpacity = 0.8;
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.contentEdgeInsets = UIEdgeInsetsMake(0.0,0.0,0.0,0.0);
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
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