将UIColor与CAShapeLayer一起使用 [英] Using UIColor with CAShapeLayer

查看:125
本文介绍了将UIColor与CAShapeLayer一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将RGB颜色传递给shapeLayer.fillColorshapeLayer.strokeColor,我的代码崩溃了.这是我尝试过的:

I'm passing an RGB color to shapeLayer.fillColor and shapeLayer.strokeColor, and my code is crashing. Here's what I tried:

let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = (UIColor(red: 57, green: 65, blue: 101, alpha: 1) as! CGColor)
shapeLayer.strokeColor = (UIColor(red: 57, green: 65, blue: 101, alpha: 1) as! CGColor)

如果我将RGB颜色写为:

If I write my RGB color as:

UIColor(red: 57, green: 65, blue: 101, alpha: 1)

Xcode警告我将其更改为:

Xcode gives me a warning to change it to:

(UIColor(red: 57, green: 65, blue: 101, alpha: 1) as! CGColor)

当我运行Xcode建议的修复程序时,我的应用程序崩溃了.为什么?

When I run Xcode's suggested fix, my app crashes. Why?

推荐答案

首先,参数redgreenbluealpha的范围必须为0.0到1.0

First of all the parameters red,green, blue and alpha must be in range from 0.0 to 1.0

其次,您必须将cgColor传递给CGLayer,例如

Secondly, you must pass the cgColor to CGLayer, e.g.

let color = UIColor(red: 57.0/255.0, green: 65.0/255.0, blue: 101.0/255.0, alpha: 1)
shapeLayer.fillColor = color.cgColor
shapeLayer.strokeColor = color.cgColor

这篇关于将UIColor与CAShapeLayer一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆