iOS 2D绘图详解(Quartz 2D)之阴影和渐变(Shadow,Gradient)

原创Blog,转载请注明出处
http://blog.youkuaiyun.com/hello_hwc?viewmode=list
我的stackoverflow

profile for Leo on Stack Exchange, a network of free, community-driven Q&A sites

前四篇基础博客路径


前言:这个系列写道这里已经是第五篇了,本文会介绍下阴影和渐变的基础知识,以及一些基本的Demo Code展示,应该还会有两篇,介绍下Bitmap绘制以及Pattern等知识。


Shadow

shadow(阴影)的目的是为了使UI更具有立体感,如图

shadow主要有三个影响因素

  • x off-set 决定阴影沿着x的偏移量
  • y off-set 决定阴影沿着y的偏移量
  • blur value 决定了阴影的边缘区域是不是模糊的

其中不同的blur效果如图

注意

Shadow也是绘制状态相关的,意味着如果仅仅要绘制一个subpath的shadow,要注意save和restore状态。

相关函数

CGContextSetShadow
CGContextSetShadowWithColor//位移区别是设置了阴影颜色

参数

  • context 绘制画板
  • offset 阴影偏移量,参考context的坐标系
  • blur 非负数,决定阴影的模糊程度
    示例代码
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddArc(context,40, 40, 20, 0,M_2_PI,0);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context,3.0);
    CGContextSetShadow(context,CGSizeMake(4.0, 4.0),1.0);
    CGContextStrokePath(context);
}
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.opaque = NO;
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.layer.borderWidth =  1.0;
    }
    return self;
}

效果
<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值