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

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

// 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)

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...