加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift 快速奔跑的兔几 本节的内容是:绘画第二讲~

发布时间:2020-12-14 01:40:23 所属栏目:百科 来源:网络整理
导读:绘制阴影的栗子: 注意需要保存和恢复上下文,否则画布上所有的绘制图形都会加上阴影。 // shadow let context = UIGraphicsGetCurrentContext() let shadowRect = CGRectInset(self .bounds ,self .bounds .size .width * 0.1 ,self .bounds .size .height

绘制阴影的栗子:
注意需要保存和恢复上下文,否则画布上所有的绘制图形都会加上阴影。

// shadow
        let context = UIGraphicsGetCurrentContext()

        let shadowRect = CGRectInset(self.bounds,self.bounds.size.width*0.1,self.bounds.size.height*0.1)

        let shadowPath = UIBezierPath(roundedRect: shadowRect,cornerRadius: 10)

        CGContextSaveGState(context)

        let shadowStyle = UIColor.blueColor().CGColor
        let shadowOffset = CGSize(width: 3,height: 3)
        let shadowBlurRadius:CGFloat = 5.0

        CGContextSetShadowWithColor(context,shadowOffset,shadowBlurRadius,shadowStyle)

        UIColor.grayColor().setFill()
        shadowPath.fill()

        CGContextRestoreGState(context)

下面是渐变的栗子:

let colorSpace = CGColorSpaceCreateDeviceRGB()

        let context = UIGraphicsGetCurrentContext()

        let gradientStartColor = UIColor(red: 0.1,green: 0.1,blue: 0.8,alpha: 1)
        let gradientEndColor = UIColor(red: 1,green: 0.6,alpha: 1)

        let gradientColors:CFArray = [gradientStartColor.CGColor,gradientEndColor.CGColor]
        let gradientLocations:[CGFloat] = [0.0,1.0]

        let gradients = CGGradientCreateWithColors(colorSpace,gradientColors,gradientLocations)

        let pathRect = CGRectInset(self.bounds,20,20)

        let topPoint = CGPointMake(self.bounds.size.width/2,20)
        let bottomPoint = CGPointMake(self.bounds.size.width/2,self.bounds.size.height - 20)

        let roundedRectPath = UIBezierPath(roundedRect: pathRect,cornerRadius: 4)

        CGContextSaveGState(context)
        roundedRectPath.addClip()
        CGContextDrawLinearGradient(context,gradients,bottomPoint,topPoint,CGGradientDrawingOptions.DrawsAfterEndLocation)
        CGContextRestoreGState(context)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读