【问题标题】:Fade out scrolling UITextView over image?在图像上淡出滚动 UITextView?
【发布时间】:2014-07-07 15:50:51
【问题描述】:

我希望在背景图像上淡出滚动的UITextView,类似于这个小示例顶部的渐变。

我正在尝试使用CAGradientLayer 和掩码来解决此问题。当视图中的文本(垂直)滚动时,我需要文本在到达视图框架之前变得透明,以产生它向上和向下淡出的错觉。

关于如何实现这一点的任何提示?

【问题讨论】:

    标签: ios core-graphics cagradientlayer


    【解决方案1】:

    哦,伙计,我经常使用这个。将其保存为 sn-p:

    CAGradientLayer *gradient = [CAGradientLayer layer];
    
    gradient.frame = self.textView.superview.bounds;
    gradient.colors = @[(id)[UIColor clearColor].CGColor, (id)[UIColor blackColor].CGColor, (id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor];
    gradient.locations = @[@0.0, @0.03, @0.97, @1.0];
    
    self.textView.superview.layer.mask = gradient;
    

    此解决方案要求您的文本视图嵌入到它自己的视图中。这将 3% 的淡入淡出应用于文本视图的顶部和底部。根据您的需要修改gradient.locations

    【讨论】:

      【解决方案2】:

      我从这个线程中受到其他人的启发,但已将代码转换为 Swift 并使用起点和终点代替,在底部制作渐变。

      if let containerView = textView.superview {
          let gradient = CAGradientLayer(layer: containerView.layer)
          gradient.frame = containerView.bounds
          gradient.colors = [UIColor.clearColor().CGColor, UIColor.blueColor().CGColor]
          gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
          gradient.endPoint = CGPoint(x: 0.0, y: 0.85)
          containerView.layer.mask = gradient
          }
      

      见:image of textView bottom with gradient

      【讨论】:

      • 对于 Swift,就可以了。谢谢!想知道是否可以同时在顶部和底部进行淡入淡出/渐变?
      • 是的,有可能。我现在可以向您确认这一点,因为我前段时间一直在这样做。我现在无法为您提供示例代码,但也许以后会有更多时间。
      • 没问题。那太好了。
      • @user4806509 只需设置位置gradient.locations = @[@0.0, @0.15, @0.85, @1.0];
      【解决方案3】:

      斯威夫特 5

      获取 Andrej 的代码并为 Swift 5 更新它

      if let containerView = textView.superview {
          let gradient = CAGradientLayer(layer: containerView.layer)
          gradient.frame = containerView.bounds
          gradient.colors = [UIColor.clear.cgColor, UIColor.blue.cgColor]
          //the line above is update for swift 5
          gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
          gradient.endPoint = CGPoint(x: 0.0, y: 0.85)
          containerView.layer.mask = gradient
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多