【问题标题】:Set gradient on UIView partially, half color is gradient and half is single color在 UIView 上部分设置渐变,一半颜色是渐变,一半是单色
【发布时间】:2016-11-18 12:54:05
【问题描述】:

尝试显示进度条使其自定义,使用 UIView 将其框架设置为进度百分比。说 20% 的框架宽度并制作渐变,但剩余的 80% 应为白色,并在其上定义百分比。

面临的问题是无法显示文本集 UILabel 而不是 UIView 但文本不显示。请指导。 以下是我尝试过的。

let view: UILabel = UILabel(frame: CGRectMake(0.0, self.scrollMainView.frame.size.height-50, self.view.frame.size.width/5, 50))
    let gradient: CAGradientLayer = CAGradientLayer()
    gradient.frame = view.bounds
    gradient.locations = [0.0 , 1.0]
    gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
    gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
    let color0 = UIColor(red:71.0/255, green:198.0/255, blue:134.0/255, alpha:1.0).CGColor
    let color1 = UIColor(red:25.0/255, green:190.0/255, blue: 205.0/255, alpha:1.0).CGColor
   // let color2 = UIColor(red:0.0/255, green:0.0/255, blue: 0.0/255, alpha:1.0).CGColor

    gradient.colors = [color1, color0]
    view.layer.insertSublayer(gradient, atIndex: 0)
    self.scrollMainView.addSubview(view)

    view.text = "20%"
    view.textColor = UIColor.blackColor()

    view.layer.shadowColor = UIColor.blackColor().CGColor
    view.layer.shadowOpacity = 1
    view.layer.shadowOffset = CGSizeZero
    view.layer.shadowRadius = 2

【问题讨论】:

    标签: iphone swift ios9


    【解决方案1】:

    我无法完全从您的问题中理解您的问题,但我将仅根据问题标题来回答。要获得半色渐变和半单色,您必须使用三种颜色渐变并相应地设置它们的位置:

    gradient.colors = [color1,color1, color0]
    gradient.locations = [0.0, 0.5, 1.0]
    

    这样,您将绘制从 color1 到 color1 的渐变(实际上是单色)并用它填充 50% 的框架区域,从 color1 到 color0 的渐变将填充框架的另一半。

    【讨论】:

      【解决方案2】:

      我回答了为什么你看不到文字。

      忘记一秒钟的层并考虑子视图。如果将子视图添加到 UILabel 会发生什么?当然,它将位于标签内容之上。

      因此,这同样适用于图层。 UILabel 在其主图层上绘制其文本,您添加到主图层的任何子图层都在其之上。

      我的建议是使用带有 CAGradientLayer 子层和 UILabel 子视图的 UIView。

      或者甚至更好,子类化 UIView 以便使用 CAGradientLayer 作为支持层(通过class func layerClass() -> AnyClass 方法),只需添加 UILabel 作为子视图。

      这是一个例子:

      class CustomView : UIView {
          lazy var label : UILabel = {
              let l = UILabel(frame: self.bounds)
              l.text = "20%"
              l.textColor = UIColor.blackColor()
              l.backgroundColor = UIColor.clearColor()
              return l
          }()
      
          override class func layerClass() -> AnyClass {
              return CAGradientLayer.self
          }
      
          override init(frame: CGRect) {
              super.init(frame: frame)
              commonInit()
          }
      
          required init?(coder aDecoder: NSCoder) {
              super.init(coder: aDecoder)
              commonInit()
          }
      
          private func commonInit() {
              let gradient: CAGradientLayer = self.layer as! CAGradientLayer
              gradient.locations = [0.0 , 1.0]
              gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
              gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
      
              let color0 = UIColor(red:71.0/255, green:198.0/255, blue:134.0/255, alpha:1.0).CGColor
              let color1 = UIColor(red:25.0/255, green:190.0/255, blue: 205.0/255, alpha:1.0).CGColor
      
              gradient.colors = [color1, color0]
      
              label.frame = bounds
              label.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
              self.addSubview(label)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-28
        • 1970-01-01
        • 2011-03-14
        • 2016-12-02
        • 1970-01-01
        • 2016-05-31
        • 1970-01-01
        • 2016-11-02
        相关资源
        最近更新 更多