绘制矩形并设置阴影

使用Core Graphics绘制阴影

图形上下文是支撑阴影的元素,这意味着你需要在上下文应用阴影,接着绘制需要阴影的形状,然后在上下文移除阴影

创建阴影的三个参数

1 需要应用阴影的图形上下文

2 偏移量

3 要应用到阴影的模糊值 

注意点 :如果在同一视图上绘制两个图形,并且想添加阴影的话,只需添加一个就可以;

代码如下

1 先创建一个单视图工程

2 再创建一个UIView 的子类 视图

3 将自己创建的子类视图添加到根视图上

4 再自己创建的子视图上补充如下代码 

#import "liView.h"


@implementation liView


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        self.backgroundColor = [UIColor whiteColor];

    }

    return self;

}

- (void)drawRect:(CGRect)rect

{

    

    [self drawRectangles:nil];

    //[self drawRectangle:nil];

    //[self drawRectangleAtBottomOfScrren:nil];

}

- (void)drawRectangle:(id)sender

{

    //创建第一条路径

    CGMutablePathRef path = CGPathCreateMutable();

    //定义矩形边界

    CGRect rectangle = CGRectMake(10.0f, 10.0f, 100.0f, 300.0f);

    //将矩形添加到自己创建的路径中

    CGPathAddRect(path, NULL, rectangle);

    

    //获得前后文

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    //设置阴影 模糊值越高 越模糊

    CGContextSetShadowWithColor(currentContext, CGSizeMake(10.0f, 10.0f), 20.0f, [[UIColor grayColor]CGColor]);

    //把这条路径添加到获得的前后文中

    CGContextAddPath(currentContext, path);

    //为矩形填充颜色

    [[UIColor yellowColor]setFill];

    //为边框设计颜色

    [[UIColor brownColor]setStroke];

    //设置线条的宽度

    CGContextSetLineWidth(currentContext, 5.0f);

    //用你正在绘制的模型绘制你的路径

    CGContextDrawPath(currentContext, kCGPathFillStroke);

    //释放路径

    CGPathRelease(path);

}


- (void)drawRectangles:(id)sender

{

    //创建路径

    CGMutablePathRef pathRef = CGPathCreateMutable();

    //初始化需要绘制的两个模型

    CGRect rectangle1 = CGRectMake(0, 0, 100, 200);

    CGRect rectangle2 = CGRectMake(220, 0, 100, 200);

    //将模型添加在数组中

    CGRect rectangles[2] = {rectangle1,rectangle2};

    //将数组添加到路径中

    CGPathAddRects(pathRef,NULL , (const CGRect*)rectangles, 2);

    //获取当前的前后文

    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    CGContextSetShadowWithColor(contextRef, CGSizeMake(10.0f, 10.0f), 20.0f, [[UIColor grayColor]CGColor]);

    //将路径添加在前后文中

    CGContextAddPath(contextRef, pathRef);

    //为矩形填充颜色

    [[UIColor yellowColor]setFill];

    //为边框设置颜色

    [[UIColor greenColor]setStroke];

    //设置边框的宽度

    CGContextSetLineWidth(contextRef, 5);

    //用将要绘制的模型来绘制路径

    CGContextDrawPath(contextRef, kCGPathFillStroke);

    //释放路径

    CGPathRelease(pathRef);

}

- (void)drawRectangleAtBottomOfScrren:(id)sender

{

    CGMutablePathRef path = CGPathCreateMutable();

    CGRect rectangle = CGRectMake(210, 10, 100, 300);

    CGPathAddRect(path, NULL, rectangle);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextAddPath(context, path);

    [[UIColor blueColor]setStroke];

    [[UIColor redColor]setFill];

    CGContextDrawPath(context, kCGPathEOFillStroke);

    CGPathRelease(path);

}


@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值