- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将 iCarousel 设置为显示一系列渐变。
// Set gradient colours
var gradientColours = [
// First gradient
UIColor(red:0.33, green:0.38, blue:0.44, alpha:1.0),
UIColor(red:1.00, green:0.42, blue:0.42, alpha:1.0),
//Second gradient
UIColor(red:0.08, green:0.12, blue:0.19, alpha:1.0),
UIColor(red:0.14, green:0.23, blue:0.33, alpha:1.0),
// Third gradient
UIColor(red:0.83, green:0.84, blue:0.16, alpha:1.0),
UIColor(red:0.83, green:0.84, blue:0.16, alpha:1.0),
// Fourth gradient
UIColor(red:0.12, green:0.11, blue:0.09, alpha:1.0),
UIColor(red:0.56, green:0.05, blue:0.00, alpha:1.0),
// Fifth gradient
UIColor(red:0.09, green:0.75, blue:0.99, alpha:1.0),
UIColor(red:0.80, green:0.19, blue:0.40, alpha:1.0)]
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
let frontView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
frontView.contentMode = .center
frontView.layer.cornerRadius = 15;
frontView.layer.masksToBounds = true;
let gradient = CAGradientLayer()
gradient.frame = frontView.bounds
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.colors = [gradientColours[index].cgColor, gradientColours[index+1].cgColor]
frontView.layer.addSublayer(gradient)
return frontView
问题是,第一个渐变很好,因为它使用了 gradientColours[0] 和 gradientColours 1 , 但之后的任何其他人都无法使用 1和 2 , 2和 [3] 等等......
我觉得这似乎有一个明显的答案,但我暂时想不起来......
感谢您的帮助。
编辑:图片清晰:
第一个渐变效果很好:
最佳答案
我通过使用简单的奇/偶逻辑来解决这个问题,请参见下面的程序
import Foundation
var gradientColours = [
// First gradient
UIColor(red:0.33, green:0.38, blue:0.44, alpha:1.0),
UIColor(red:1.00, green:0.42, blue:0.42, alpha:1.0),
//Second gradient
UIColor(red:0.08, green:0.12, blue:0.19, alpha:1.0),
UIColor(red:0.14, green:0.23, blue:0.33, alpha:1.0),
// Third gradient
UIColor(red:0.83, green:0.84, blue:0.16, alpha:1.0),
UIColor(red:0.83, green:0.84, blue:0.16, alpha:1.0),
// Fourth gradient
UIColor(red:0.12, green:0.11, blue:0.09, alpha:1.0),
UIColor(red:0.56, green:0.05, blue:0.00, alpha:1.0),
// Fifth gradient
UIColor(red:0.09, green:0.75, blue:0.99, alpha:1.0),
UIColor(red:0.80, green:0.19, blue:0.40, alpha:1.0)]
var indexOfGradientColor = 0
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView
{
let frontView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
frontView.contentMode = .center
frontView.layer.cornerRadius = 15;
frontView.layer.masksToBounds = true;
let gradient = CAGradientLayer()
gradient.frame = frontView.bounds
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
if (indexOfGradientColor%2 == 0)
{
gradient.colors = [gradientColours[indexOfGradientColor].cgColor, gradientColours[indexOfGradientColor+1].cgColor]
}
else
{ gradient.colors = [gradientColours[indexOfGradientColor+1].cgColor, gradientColours[indexOfGradientColor+2].cgColor]}
if indexOfGradientColor < (gradientColours.count-2)
{
indexOfGradientColor += 2
}
else
{
indexOfGradientColor = 0
}
frontView.layer.addSublayer(gradient)
return frontView
}
关于ios - CAGradient 使用 UIColor 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44962061/
我正在为 UIColor 编写一个简单的扩展,以获取基于 this answer 的十六进制字符串: import UIKit extension UIColor { public stati
例如,下面的代码与 UIColor.Purple 或 UIColor.Yellow 一起工作正常,但当我使用 UIColor.black 时应用程序崩溃。 fatal error :索引超出范围使用
这个颜色 [UIColor colorWithRed:75.0/255.0 green:215.0/255.0 blue:100.0/
使用 self.backgroundColor = .init(named: "my-color") 其中 self 是一个 UIView,触发此错误: Value of optional type
我正在尝试使用以下代码创建一个 UIColor 实例: var someColor:UIColor = UIColor.colorWithAlphaComponent(0.3) 预编译器告诉我有错误。
我有一个带有 UIColor 属性的对象: class Beer: NSObject { var color: UIColor? ... } 我正在将其保存到数据库中,因此我需要将此属性转换为
这个问题已经有答案了: Swift UIColor initializer - compiler error only when targeting iPhone5s (2 个回答) 已关闭 8 年前
我在 View Controller 类中设置标签颜色。当我使用以下代码时,它起作用了。 self.labelTest.textColor = UIColor.red 但是,如果我使用 UIColor
我想在饼图中提供自定义颜色(使用图表 Pane )。但是为此,setColors([NSUIColor]) 中的数组需要 NSUIColor 格式的颜色数组,我有颜色的十六进制代码。如何使用十六进制代
我正在尝试从 rgb-hex 色彩空间实现 CIColor,如下所示: let bottomColor = UIColor.init(red: 235/255, green: 250/255,
在我的应用程序中,我尝试使用不同的颜色进行绘制。我有自己的颜色选择器,但我想将颜色亮度添加到此选择器 slider 。对于 understudying :我选择一种颜色显示在 View 中的某个地方作
UILabel label = new UILabel(); label.TextColor = UIColor.Black; 如何以字符串格式存储 UILabel 颜色?我想从数据库中获取该颜色并
我无法从使用工厂方法创建的 UIColor(带有模式)创建 NSData 对象 [UIColor colorWithPatternImage:image] 适用于标准 UIColor 对象。想知道是否
我在将 uicolor 转换为十六进制颜色时遇到问题,这是我发现的 CGColorRef colorref = [[Colorview_ backgroundColor] CGColor]; int
我正在绘制一个饼图,每个切片都有不同的颜色。我需要给切片带来纹理外观,而不仅仅是纯色。有什么想法如何做到这一点?我不想使用图像作为所有可能颜色的纹理。所以我需要生成纹理或类似的东西。有任何想法吗。谢谢
我有一个十六进制颜色值,我想将其转换为 UIColor。 UIColor 可以接受色相/饱和度/亮度 (HSB) 值或红/蓝/绿 (RGB) 值。如何使用十六进制值创建 UIColor? 最佳答案 有
我正在涉足 Android 的 MonoTouch/Mono,目前正在创建我自己的 UIColor 子类。不知何故,我无法让我的工厂方法以我想要的方式工作(但我确实有一个解决方法)。 以下代码导致我崩
我已经构建了一组自定义颜色以在我的应用程序中使用,例如石灰 mylimeColor = [UIColor colorWithRed:0.502 green:1.000 blue:0.000 alpha
我正在制作一个交互式应用程序,它应该显示一个蓝色的 UILabel,当按下某个按钮时它必须消失。当我们继续按下按钮时,标签的颜色应该会褪色。我尝试创建自定义 UIColor 并更改其亮度,但效果并不理
尝试简化每个单元格行对象着色的实现。这就是我当前向每行中的对象添加颜色的方式: progressViewLeft.primaryColor = Colors.Stage1 “Colors”是一个像这样
我是一名优秀的程序员,十分优秀!