gpt4 book ai didi

ios - 将渐变应用于 UIView 的一个角

转载 作者:行者123 更新时间:2023-11-28 06:16:53 25 4
gpt4 key购买 nike

我有一个 UIView,我在其中显示带有 AVPlayer 的视频。我需要在其中一个角显示视频的时间,为此我需要向其中一个角添加黑色渐变,以便始终可以看到白色时间。​​

像这样:

Sample

我的想法是在带有视频的 UIView 之上添加一个 UIView (ViewA),并将渐变添加到 ViewA,并将 UILabel 添加到该 View ...

我的问题是我不知道如何在从 UIColor.clear 到 UIColor.black 的一个角上添加渐变。

任何线索,想法如何只将渐变应用于其中一个角?

谢谢!

最佳答案

将其添加到您的 UIView,您可以通过编程方式以及从 Storyboard 中更改参数。

@IBDesignable
class GradientView: UIView {

@IBInspectable var startColor: UIColor = .black { didSet { updateColors() }}
@IBInspectable var endColor: UIColor = .white { didSet { updateColors() }}
@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() }}

override class var layerClass: AnyClass { return CAGradientLayer.self }

var gradientLayer: CAGradientLayer { return layer as! CAGradientLayer }

func updatePoints() {
if horizontalMode {
gradientLayer.startPoint = diagonalMode ? CGPoint(x: 1, y: 0) : CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = diagonalMode ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0.5)
} else {
gradientLayer.startPoint = diagonalMode ? CGPoint(x: 0, y: 0) : CGPoint(x: 0.5, y: 0)
gradientLayer.endPoint = diagonalMode ? CGPoint(x: 1, y: 1) : CGPoint(x: 0.5, y: 1)
}
}
func updateLocations() {
gradientLayer.locations = [startLocation as NSNumber, endLocation as NSNumber]
}
func updateColors() {
gradientLayer.colors = [startColor.cgColor, endColor.cgColor]
}

override func layoutSubviews() {
super.layoutSubviews()
updatePoints()
updateLocations()
updateColors()
}
}

关于ios - 将渐变应用于 UIView 的一个角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44866239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com