Adding Shadows to Shapes(添加阴影)

绘制过程:

    创建路径 -CGMutablePathRef  CGPathCreateMutable (void);  //CGMutablePathRef path = CGPathCreateMutable();

    设置矩形 -CGRect CGRectMake (CGFloat x,CGFloat y,CGFloat width,CGFloat height);

    将所要绘制的图形(矩形)加入路径 

        加入单个矩形 -void  CGPathAddRect (CGMutablePathRef path,const CGAffineTransform *m,CGRect rect); //第二个参数:NULL

        加入多个矩形 -void  CGPathAddRects (CGMutablePathRef path,const CGAffineTransform *m,const CGRect rects[],size_t count);

    创建上下文环境 -CGContextRef  UIGraphicsGetCurrentContext (void); //CGContextRef currentContext = UIGraphicsGetCurrentContext();

        在环境中添加路径 - void   CGContextAddPath  (CGContextRef context,CGPathRef path);

    保存状态 -void CGContextSaveGState (CGContextRef c);  // CGContextSaveGState(currentContext); 

    设置相关属性 

       - (void)setFill  // 填充颜色

       - (void)setStroke  //边界颜色  

       - void CGContextSetLineWidth (CGContextRef c,CGFloat width); //线宽

       - void  CGContextSetShadowWithColor (CGContextRef context,CGSize offset,CGFloat blur,CGColorRef color); //设置阴影(宽度,模糊度,颜色)

    绘制路径 -void  CGContextDrawPath (CGContextRef c,CGPathDrawingMode mode);

    释放 -void  CGPathRelease (CGPathRef path); 

    恢复初始状态 -void CGContextRestoreGState (CGContextRef c);  //CGContextRestoreGState(currentContext);


p.s. 

   默认情况下,上下文环境对象(currentContext)中的属性可一直保存相同设置(如颜色,线宽,阴影等)

   若想使用不同设置可使用 CGContextSaveGState , CGContextRestoreGState


- (void)drawRect:(CGRect)rect{

    /* Create the path first. */

    CGMutablePathRef path = CGPathCreateMutable();


    CGRect firstRect = CGRectMake(55.0f,60.0f,150.0f,150.0f);


    /* Add the rectangle to the path */

   CGPathAddRect(path, NULL,firstRect);


    /* Get the handle to the current context */

    CGContextRef currentContext = UIGraphicsGetCurrentContext();


    /* Add the path to the context */

   CGContextAddPath(currentContext,path);


   CGContextSaveGState(currentContext);


   CGContextSetShadowWithColor(currentContext,

                                CGSizeMake(50.0f, 10.0f), //阴影宽度(右,下)

                                10.0f,  //CGFloat,阴影模糊度 值越大,越模糊,=0:为实形

                                [[UIColor redColor] CGColor]); //阴影颜色

    /* Set the fill color to cornflower blue */

    [[UIColor colorWithRed:0.20f

                     green:0.60f

                      blue:0.80f

                     alpha:1.0f]setFill];


    /* Fill the path on the context */

   CGContextDrawPath(currentContext,kCGPathFill);


    /* Dispose of the path */

   CGPathRelease(path);

  

    /* Restore the context to how it was when we started */

    // 保证在其它方法中使用 UIGraphicsGetCurrentContext() 时,内部的属性(如颜色,线宽,阴影等)不会使用上述设置

    CGContextRestoreGState(currentContext);

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值