我有3个班级,分别是ShopViewController
,ShopCellClass
和ShopClass
我以编程方式建立自己的商店,我想插入由ShopClass提供的两种颜色的渐变。问题是最后一个渐变颜色不好。.我向您展示我的3个类的代码:
ShopViewController
:
class ShopViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(ShopCellClass.self, forCellReuseIdentifier: "CellShop")
tableView.rowHeight = 155
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shop.data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"CellShop") as! ShopCellClass
cell.drawTheCard(index: indexPath.row)
return cell
}
}
我的ShopCellClass
:
class ShopCellClass : UITableViewCell {
var widthOfCard : CGFloat = 0
var heightOfCard : CGFloat = 0
let cardView = UIView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(cardView)
widthOfCard = UIScreen.main.bounds.size.width
heightOfCard = 155
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func drawTheCard(index : Int){
drawCard(index: index)
}
private func drawCard(index: Int){
cardView.frame = CGRect(x : 0, y : 0, width : widthOfCard, height : heightOfCard)
let gradientBg = UIView()
gradientBg.frame = cardView.bounds
let leftColor = shop.data[index].leftColor
let rightColor = shop.data[index].rightColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [leftColor.cgColor,
rightColor.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 1)
gradientLayer.endPoint = CGPoint(x: 1, y: 0)
gradientLayer.frame = CGRect.init(
x: cardView.bounds.origin.x,
y: cardView.bounds.origin.y,
width: widthOfCard,
height: heightOfCard)
gradientLayer.masksToBounds = false
cardView.layer.insertSublayer(gradientLayer, at: 0)
}
}
和我的ShopClass
:
class ShopClass {
var data: [(id : Int, name : String, price : Double, leftColor : UIColor, rightColor : UIColor)] = []
init(){
self.loadShopData()
}
func loadShopData(){
data.append((id : 1, name : "Pack 1", price : 4.99, leftColor : UIColor(rgb: 0xA46628), rightColor : UIColor(rgb: 0xE08B37))) // Brown
data.append((id : 2, name : "Pack 2", price : 9.99, leftColor : UIColor(rgb: 0x8C8C8C), rightColor : UIColor(rgb: 0xAFAFAF))) // Gray
data.append((id : 3, name : "Pack 3", price : 15.99, leftColor : UIColor(rgb: 0xF3B700), rightColor : UIColor(rgb: 0xF3CD00))) // Yellow
data.append((id : 4, name : "Pack 4", price : 19.99, leftColor : UIColor(rgb: 0x980000), rightColor : UIColor(rgb: 0xF20000))) // Red
data.append((id : 5, name : "Pack 5", price : 24.99, leftColor : UIColor(rgb : 0x000000), rightColor : UIColor(rgb : 0x2C2C2C))) // Black
}
}
let shop = ShopClass()
通常,由ShopClass
定义的渐变颜色为:
->棕色->灰色->黄色->红色->黑色
但是我有:
->棕色->灰色->黄色->黑色//错误->棕色//错误
喜欢显示gif:
所以我的问题是:
为什么我的最后两种颜色是错误的?
错误显示在Iphone 8模拟器中,而不是iphoneX。也许是rowHeight的问题?
感谢您的回复