Here is a subclass that not only creates a shadow, but is also animated when a button is 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
avance Jul 06 '11 at 10:18 2011-07-06 22:18
source share