将渐变背景添加到可滚动的UITextView

时间:2018-08-18 13:49:57

标签: ios swift uitextview cagradientlayer

将渐变作为背景添加到iOS中的视图时,我采用了以下方法:

let gradient = CAGradientLayer()
// gradient settings...
view.layer.insertSublayer(gradient, at: 0)

但是,在可滚动的UITextView 上进行渐变时,只会在可见范围内绘制渐变,然后在文本视图向上滚动时向上滚动并退出视图

此外,由于 UITextView没有固定的内容大小,因此也无法为此设置渐变,尽管这也不理想。

一种解决方法是将另一个视图放置在滚动视图后面,然后在其中应用渐变,然后将文本视图放置在该视图上方并使背景透明。 但这不是首选的解决方案。

问: 有什么方法可以固定渐变层的位置,使其不随文本层滚动?


编辑1:附加代码:

let view = UITextView(frame: CGRect(x: 20, y: 20, width: 200, height: 200))
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = view.bounds
gradient.colors = [gradientColors]  // pre-set array of 2 CGColors
let alpha: Float = angle / 360  // pre-set between 0-90
let startPointX = powf(sinf(2 * Float.pi * ((alpha + 0.75) / 2)), 2)
let startPointY = powf(sinf(2 * Float.pi * ((alpha + 0) / 2)), 2)
let endPointX = powf(sinf(2 * Float.pi * ((alpha + 0.25) / 2)), 2)
let endPointY = powf(sinf(2 * Float.pi * ((alpha + 0.5) / 2)), 2)
gradient.endPoint = CGPoint(x: CGFloat(endPointX), y: CGFloat(endPointY))
gradient.startPoint = CGPoint(x: CGFloat(startPointX), y: CGFloat(startPointY))
view.layer.insertSublayer(gradient, at: 0)

编辑2:解决方法(有效,但渐变仅在文本视图获得焦点后可见):

extension UITextView {      
  func addGradient(gradient: CAGradientLayer) {
    backgroundColor = UIColor.clear
    let gradientContainer = UIView(frame:
      CGRect(x: frame.minX, y: frame.minY,
             width: frame.width, height: frame.height))
    gradientContainer.isUserInteractionEnabled = false
    gradientContainer.layer.insertSublayer(gradient, at: 0)
    superview?.addSubview(gradientContainer)
  }
}

let view = UITextView(frame: CGRect(x: 20, y: 20, width: 200, height: 200))
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = view.bounds
gradient.colors = [gradientColors]  // pre-set array of 2 CGColors
let alpha: Float = angle / 360  // pre-set between 0-90
let startPointX = powf(sinf(2 * Float.pi * ((alpha + 0.75) / 2)), 2)
let startPointY = powf(sinf(2 * Float.pi * ((alpha + 0) / 2)), 2)
let endPointX = powf(sinf(2 * Float.pi * ((alpha + 0.25) / 2)), 2)
let endPointY = powf(sinf(2 * Float.pi * ((alpha + 0.5) / 2)), 2)
gradient.endPoint = CGPoint(x: CGFloat(endPointX), y: CGFloat(endPointY))
gradient.startPoint = CGPoint(x: CGFloat(startPointX), y: CGFloat(startPointY))

view.addGradient(gradient)

编辑3: Carpsen90 的创造性解决方法(谢谢!):

class ViewController: UIViewController, UITextViewDelegate {

  @IBOutlet weak var myTxtView: UITextView!
  var gradient = CAGradientLayer()

  override func viewDidLoad() {
    super.viewDidLoad()
    myTxtView.delegate = self
    gradient.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]
    updateGradientFrame()
    myTxtView.textInputView.layer.insertSublayer(gradient, at: 0)
  }

  func textViewDidChange(_ textView: UITextView) {
    updateGradientFrame()
  }

  func updateGradientFrame() {
    gradient.frame = myTxtView.textInputView.bounds
  }

}

除了:

i。

键入时由于渐变刷新引起轻微闪烁

ii。回车超过文本视图底部时,有时会导致渐变向上移位,从而导致文本视图底部出现空白

iii。在输入边界并向下滚动到文本视图边界之后,当向上滚动到文本视图的顶部时,渐变会保留在 contentsize区域的底部;显然,滚动不会触发 textViewDidChange()委托方法。

1 个答案:

答案 0 :(得分:1)

这是一种解决方法:让VC观察textView中的更改,并相应地更新渐变框架:

class ViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var myTxtView: UITextView!

    var gradient = CAGradientLayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        //set the delegate to self
        myTxtView.delegate = self
        //set up the gradient
        gradient.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]
        updateGradientFrame()
        //insert the gradient in the textInputView
        myTxtView.textInputView.layer.insertSublayer(gradient, at: 0)
    }

    //The textView delegate method
    func textViewDidChange(_ textView: UITextView) {
        updateGradientFrame()
    }

    func updateGradientFrame() {
        gradient.frame = myTxtView.textInputView.bounds
    }
}