答案 0 :(得分:2)
您可以使用CAGradientLayer。虽然结果并不完全相同,但它比按钮大小更改时设置背景图像更有优势。
以下代码已准备好用于Xcode Playground:
(units * unit_price) / (enddate - startdate in days)
答案 1 :(得分:0)
Swift 2.3
的代码变化不大button.backgroundColor = UIColor.darkGrayColor()
let gradientLayer = CAGradientLayer()
gradientLayer.frame = button.bounds
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5) //Set the start point to the left edge center
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5) //Set end point to the right edge center
gradientLayer.colors = [UIColor.clearColor().CGColor, UIColor.redColor().colorWithAlphaComponent(0.5).CGColor, UIColor.redColor().colorWithAlphaComponent(0.5).CGColor, UIColor.clearColor().CGColor]
gradientLayer.locations = [0, 0.2, 0.8, 1] //At the start point, the clear color is used; at 20% horizontal, red is used, etc
button.layer.insertSublayer(gradientLayer, atIndex: 0)