微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐