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

前端之家收集整理的这篇文章主要介绍了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)

猜你在找的Swift相关文章