iOS 图表的 Swift 渐变填充
Gradient Fill in Swift for iOS-Charts
我正在尝试创建一个漂亮的渐变填充,如 ios-图表页面上的演示所示。但是,我无法在 swift vs obj-c 中解决这个问题。
这是 obj-c 代码:
NSArray *gradientColors = @[
(id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
(id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
];
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
set1.fillAlpha = 1.f;
set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
现在这是我在 swift 中的版本:
let gradColors = [UIColor.cyanColor().CGColor, UIColor.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)]
let colorLocations:[CGFloat] = [0.0, 1.0]
let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), gradColors, colorLocations)
我缺少的是:
图表填充 fillWithLinearGradient
我在任何地方都找不到 pod 中的 fillWithLinearGradient,所以我有点困惑。
在此先感谢您的帮助!
罗布
我相信这就是您要查找的代码。您应该能够在 Swift 中使用相同的 ChartFill
class 来设置 set1.fill
.
let gradColors = [UIColor.cyanColor().CGColor, UIColor.clearColor.CGColor]
let colorLocations:[CGFloat] = [0.0, 1.0]
if let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), gradColors, colorLocations) {
set1.fill = ChartFill(linearGradient: gradient, angle: 90.0)
}
确保使用最新版本的 ios-charts。如果你使用 CocoaPods 安装 ios-charts 改变这个:
pod 'Charts'
至此
pod 'Charts', '~> 2.2'
这非常有效 (Swift 3.1 和 ios-charts 3.0.1)
let gradientColors = [UIColor.cyan.cgColor, UIColor.clear.cgColor] as CFArray // Colors of the gradient
let colorLocations:[CGFloat] = [1.0, 0.0] // Positioning of the gradient
let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations) // Gradient Object
yourDataSetName.fill = Fill.fillWithLinearGradient(gradient!, angle: 90.0) // Set the Gradient
set.drawFilledEnabled = true // Draw the Gradient
结果
对于swift4
let colorTop = UIColor(red: 255.0/255.0, green: 149.0/255.0, blue: 0.0/255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 255.0/255.0, green: 94.0/255.0, blue: 58.0/255.0, alpha: 1.0).cgColor
let gradientColors = [colorTop, colorBottom] as CFArray
let colorLocations:[CGFloat] = [0.0, 1.0]
let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations) // Gradient Object
yourDataSetName.fill = Fill.fillWithLinearGradient(gradient!, angle: 90.0)
对于 Swift 5 使用以下内容:
yourDataSetName.fill = LinearGradientFill(....
or
yourDataSetName.fill = ColorFill(...
您可以在此处阅读更多详细信息:
Charts
我正在尝试创建一个漂亮的渐变填充,如 ios-图表页面上的演示所示。但是,我无法在 swift vs obj-c 中解决这个问题。 这是 obj-c 代码:
NSArray *gradientColors = @[
(id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
(id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
];
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
set1.fillAlpha = 1.f;
set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
现在这是我在 swift 中的版本:
let gradColors = [UIColor.cyanColor().CGColor, UIColor.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)]
let colorLocations:[CGFloat] = [0.0, 1.0]
let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), gradColors, colorLocations)
我缺少的是: 图表填充 fillWithLinearGradient
我在任何地方都找不到 pod 中的 fillWithLinearGradient,所以我有点困惑。
在此先感谢您的帮助! 罗布
我相信这就是您要查找的代码。您应该能够在 Swift 中使用相同的 ChartFill
class 来设置 set1.fill
.
let gradColors = [UIColor.cyanColor().CGColor, UIColor.clearColor.CGColor]
let colorLocations:[CGFloat] = [0.0, 1.0]
if let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), gradColors, colorLocations) {
set1.fill = ChartFill(linearGradient: gradient, angle: 90.0)
}
确保使用最新版本的 ios-charts。如果你使用 CocoaPods 安装 ios-charts 改变这个:
pod 'Charts'
至此
pod 'Charts', '~> 2.2'
这非常有效 (Swift 3.1 和 ios-charts 3.0.1)
let gradientColors = [UIColor.cyan.cgColor, UIColor.clear.cgColor] as CFArray // Colors of the gradient
let colorLocations:[CGFloat] = [1.0, 0.0] // Positioning of the gradient
let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations) // Gradient Object
yourDataSetName.fill = Fill.fillWithLinearGradient(gradient!, angle: 90.0) // Set the Gradient
set.drawFilledEnabled = true // Draw the Gradient
结果
对于swift4
let colorTop = UIColor(red: 255.0/255.0, green: 149.0/255.0, blue: 0.0/255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 255.0/255.0, green: 94.0/255.0, blue: 58.0/255.0, alpha: 1.0).cgColor
let gradientColors = [colorTop, colorBottom] as CFArray
let colorLocations:[CGFloat] = [0.0, 1.0]
let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations) // Gradient Object
yourDataSetName.fill = Fill.fillWithLinearGradient(gradient!, angle: 90.0)
对于 Swift 5 使用以下内容:
yourDataSetName.fill = LinearGradientFill(....
or
yourDataSetName.fill = ColorFill(...
您可以在此处阅读更多详细信息: Charts