【发布时间】:2018-12-02 19:10:17
【问题描述】:
我在为 UIView 设置渐变的位置定义了以下扩展文件:
import Foundation
import UIKit
extension UIView {
func setGradientBackground(colorOne: UIColor, colorTwo: UIColor) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = [colorOne.blue, colorTwo.red]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.startPoint = CGPoint(x: 1.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 0.0, y: 0.0)
layer.insertSublayer(gradientLayer, at: 0)
}
这链接到一个单独的结构文件,我在其中定义了四个UIColors:
blue,red,black,white
我的 viewcontroller.swift 文件在viewDidLoad 中有以下代码:
myView.setGradientBackground(colorOne: Colors.blue, colorTwo: Colors.red)
这似乎工作正常。
我想要实现的是,当我点击我的IBAction 按钮时,它会将myView 颜色更改为黑色和白色(其余两种颜色)。所以我尝试了这个:
@IBAction func hitButton(_ sender: Any) {
myView.setGradientBackground(colorOne: Colors.black, colorTwo: Colors.white)
}
但这会导致崩溃:“无法识别的选择器发送到实例 0x7fb25060a120”
【问题讨论】:
标签: swift cagradientlayer