android 灰色向白色渐变,iPhone-iOS的白色到透明渐变层为灰色

尽管使用了清晰的白色,但还是有很多灰色。

因此,我改变了方法,选择了遮罩而不是渐变。 最终结果是相同的,更好,更好,因为这一方法在所有情况下都有效,而不仅仅是您拥有合适的背景。

我没有在IB上尝试此代码,但希望它也能正常工作。只需设置backgroundColor,您就可以使用了。

@IBDesignable

class FadingView: UIView {

@IBInspectable var startLocation: Double = 0.05 { didSet { updateLocations() }}

@IBInspectable var endLocation: Double = 0.95 { didSet { updateLocations() }}

@IBInspectable var horizontalMode: Bool = false { didSet { updatePoints() }}

@IBInspectable var diagonalMode: Bool = false { didSet { updatePoints() }}

@IBInspectable var invertMode: Bool = false { didSet { updateColors() }}

private let gradientLayerMask = CAGradientLayer()

private func updatePoints() {

if horizontalMode {

gradientLayerMask.startPoint = diagonalMode ? CGPoint(x: 1, y: 0) : CGPoint(x: 0, y: 0.5)

gradientLayerMask.endPoint = diagonalMode ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0.5)

} else {

gradientLayerMask.startPoint = diagonalMode ? CGPoint(x: 0, y: 0) : CGPoint(x: 0.5, y: 0)

gradientLayerMask.endPoint = diagonalMode ? CGPoint(x: 1, y: 1) : CGPoint(x: 0.5, y: 1)

}

}

private func updateLocations() {

gradientLayerMask.locations = [startLocation as NSNumber, endLocation as NSNumber]

}

private func updateSize() {

gradientLayerMask.frame = bounds

}

private func updateColors() {

gradientLayerMask.colors = invertMode ? [UIColor.white.cgColor, UIColor.clear.cgColor] : [UIColor.clear.cgColor, UIColor.white.cgColor]

}

private func commonInit() {

layer.mask = gradientLayerMask

}

override init(frame: CGRect) {

super.init(frame: frame)

commonInit()

}

required init?(coder aDecoder: NSCoder) {

super.init(coder: aDecoder)

commonInit()

}

override func layoutSubviews() {

super.layoutSubviews()

updatePoints()

updateLocations()

updateSize()

updateColors()

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值