Ios 仅对视图的一个角应用渐变
我有一个UIView,在那里我用AVPlayer显示视频。我需要在其中一个角上显示视频的时间,为此我需要在其中一个角上添加一个黑色渐变,以便始终可以看到白色的时间 大概是这样的: 我的想法是在视频的UIView顶部添加一个UIView(ViewA),并将渐变添加到ViewA,同时将UILabel添加到该视图中 我的问题是,我不知道如何在从UIColor.clear到UIColor.black的一个角添加渐变 有没有线索,知道如何只在其中一个角上应用渐变Ios 仅对视图的一个角应用渐变,ios,swift,uikit,cagradientlayer,Ios,Swift,Uikit,Cagradientlayer,我有一个UIView,在那里我用AVPlayer显示视频。我需要在其中一个角上显示视频的时间,为此我需要在其中一个角上添加一个黑色渐变,以便始终可以看到白色的时间 大概是这样的: 我的想法是在视频的UIView顶部添加一个UIView(ViewA),并将渐变添加到ViewA,同时将UILabel添加到该视图中 我的问题是,我不知道如何在从UIColor.clear到UIColor.black的一个角添加渐变 有没有线索,知道如何只在其中一个角上应用渐变 谢谢 将其添加到UIView中,您可以通
谢谢 将其添加到UIView中,您可以通过编程方式以及从情节提要更改参数
@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()
}
}
如果您知道如何使用CoreImage过滤器,则使用
CIRadialGradient
就可以了。我从未使用过它,但它看起来像是你可以选择一个足够大的半径,让四个角如你所愿——从这里开始,更多的是你的UIView和/或Calayer层次结构的问题。