怎么在ios代码控制出现控件的阴影

悟途网 2013年06月06日 22:24 阅读()
字号 (A- A+)

只需要把对应的空间,进行重写。开发的主要针对UIButton这个控件

.h文件,如下:

#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>

@interface ShadowButton : UIButton

{

UIColor *shadowColor;

}

@property(nonatomic , strong)UIColor *shadowColor;

@end

.m文件,如下
#import "ShadowButton.h"


@implementation ShadowButton

@synthesize shadowColor;


-(void)setProperty

{

self.imageEdgeInsets = UIEdgeInsetsMake(0, -1, 3, 2);

self.shadowColor = [UIColor grayColor];

}


- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

// Initialization code

}

return self;

}


-(id)initWithCoder:(NSCoder *)aDecoder

{

self = [super initWithCoder:aDecoder];

if (self) {

[self setProperty];

}

return self;

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

// Drawing code

CGContextRef context = UIGraphicsGetCurrentContext();

CGRect frame = rect;

UIEdgeInsets insets = self.imageEdgeInsets;

frame.origin.x +=insets.left;

frame.origin.y +=insets.top;

frame.size.width -= (insets.left + insets.right);

frame.size.height -= (insets.top + insets.bottom);

if (shadowColor) {

CGContextSetShadowWithColor(context, CGSizeMake(insets.right, insets.bottom), 10, [shadowColor CGColor]);

}

UIImage *image = self.imageView.image;

[image drawInRect:frame];

}

@end

热门文章
随机推荐
iPhone手机这些功能你都知道吗?iPhone使用技巧大汇总

iPhone手机这些功能你都知道

苹果喜欢让功能保持简单。这是一件很好的事情,它允...

iOS 8.4.1升级吗? iphone6更新iOS 8.4.1的实际体验

iOS 8.4.1升级吗? iphone6更新

iOS 8.4.1正式版已经发布了,相信有的朋友已经完成了...

iPhone手机存储满了?iOS 10的18个技巧解决存储问题

iPhone手机存储满了?iOS 10的

虽然说苹果推出的iPhone 7配备了至少32 GB的存储空间。但...

Apple ID被盗?这样设置足够保证无人盗号

Apple ID被盗?这样设置足够

Apple ID被盗?不管是自己的邮箱还是appleid密码,请设置...

如何绕过“查找我的iPhone”?小偷们利用私有DNS是一个办法

如何绕过“查找我的iPhone”

关闭“寻找我的iPhone”功能,不需要什么验证,苹果在...

怎么设置iPhone 5S朗读文字功能

怎么设置iPhone 5S朗读文字功

iPhone 5S手机朗读文字设置教程,1、首先在手机桌面打开...

iPhone 7手机掉电严重问题的解决方法

iPhone 7手机掉电严重问题的

请各位iPhone 7手机掉电严重的同学,看看自己的手机的...

苹果iPhone 7 Plus手机清理应用缓存的教程

苹果iPhone 7 Plus手机清理应用

手机用久了,总感觉有很多垃圾需要清理,再牛的苹果...