ios 12 (Swift 4.2) 后按钮渐变不起作用
Button gradient not working after ios 12 (Swift 4.2)
随着 ios 12 和 swift 4.2 的到来,我的代码不再有效。它曾经是从左到右,深紫色到浅紫色的渐变按钮。我该如何解决这个问题?
// Gives the button gradient values
func setGradientButton(colorOne: UIColor, colorTwo: UIColor, x1: Double, y1: Double, x2: Double, y2: Double) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]
gradientLayer.locations = [0.0, 0.0]
gradientLayer.startPoint = CGPoint(x: x1, y: y1)
gradientLayer.endPoint = CGPoint(x: x2, y: y2)
layer.insertSublayer(gradientLayer, at: 0)
}
// Sets UI elements
func setUI(_ label : UILabel, _ button : UIButton) {
let colorOne = UIColor(red: 119.0 / 255.0, green: 85.0 / 255.0, blue: 254.0 / 255.0, alpha: 100.0)
let colorTwo = UIColor(red: 177.0 / 255.0, green: 166.0 / 255.0, blue: 251.0 / 255.0, alpha: 100.0)
button.setGradientButton(colorOne: colorOne, colorTwo: colorTwo, x1: 0.0, y1: 50, x2: 150, y2: 50)
button.layer.cornerRadius = 4
button.clipsToBounds = true
}
我无法假设您的代码之前为何有效,但我发现您的 CAGradientLayer
设置存在一些问题。
首先是locations
数组。根据 Apple Documentation 这个值“ 定义了每个梯度停止点的位置 ”。所以如果你想让你的渐变以一种颜色开始并以另一种颜色结束,你需要像这样设置你的locations
gradientLayer.locations = [0.0, 1.0]
另一个问题是startPoint
和endPoint
。同样,来自 documentation:
The point is defined in the unit coordinate space and is then mapped to the layer’s bounds rectangle when drawn.
因此您的积分值应介于 0.0
和 1.0
之间。在单位坐标 space 中,(0.0, 0.0)
是视图的左上角,(1.0, 1.0)
是右下角。如果你想得到水平渐变,你需要这样设置点
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
希望对你有帮助。
随着 ios 12 和 swift 4.2 的到来,我的代码不再有效。它曾经是从左到右,深紫色到浅紫色的渐变按钮。我该如何解决这个问题?
// Gives the button gradient values
func setGradientButton(colorOne: UIColor, colorTwo: UIColor, x1: Double, y1: Double, x2: Double, y2: Double) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]
gradientLayer.locations = [0.0, 0.0]
gradientLayer.startPoint = CGPoint(x: x1, y: y1)
gradientLayer.endPoint = CGPoint(x: x2, y: y2)
layer.insertSublayer(gradientLayer, at: 0)
}
// Sets UI elements
func setUI(_ label : UILabel, _ button : UIButton) {
let colorOne = UIColor(red: 119.0 / 255.0, green: 85.0 / 255.0, blue: 254.0 / 255.0, alpha: 100.0)
let colorTwo = UIColor(red: 177.0 / 255.0, green: 166.0 / 255.0, blue: 251.0 / 255.0, alpha: 100.0)
button.setGradientButton(colorOne: colorOne, colorTwo: colorTwo, x1: 0.0, y1: 50, x2: 150, y2: 50)
button.layer.cornerRadius = 4
button.clipsToBounds = true
}
我无法假设您的代码之前为何有效,但我发现您的 CAGradientLayer
设置存在一些问题。
首先是locations
数组。根据 Apple Documentation 这个值“ 定义了每个梯度停止点的位置 ”。所以如果你想让你的渐变以一种颜色开始并以另一种颜色结束,你需要像这样设置你的locations
gradientLayer.locations = [0.0, 1.0]
另一个问题是startPoint
和endPoint
。同样,来自 documentation:
The point is defined in the unit coordinate space and is then mapped to the layer’s bounds rectangle when drawn.
因此您的积分值应介于 0.0
和 1.0
之间。在单位坐标 space 中,(0.0, 0.0)
是视图的左上角,(1.0, 1.0)
是右下角。如果你想得到水平渐变,你需要这样设置点
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
希望对你有帮助。