gpt4 book ai didi

ios - 向可滚动的 UITextView 添加渐变背景

转载 作者:行者123 更新时间:2023-11-28 05:54:20 25 4
gpt4 key购买 nike

在 iOS 中为 View 添加渐变背景时,我采用了这种方法:

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

但是,当在可滚动的 UITextView 上这样做时,渐变仅在可见边界内绘制,然后在 TextView 向上滚动时向上滚动并超出 View

此外,由于 UITextView 没有固定的内容大小,因此也不可能将渐变设置为此,尽管这也不是我们想要的。

一个解决方法是在 ScrollView 后面放置另一个 View 并在那里应用渐变,然后将 TextView 放置在它上面并使其背景透明。 但这不是首选解决方案。

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


编辑 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:解决方法(可行,但渐变仅在 TextView 获得焦点后可见):

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
}

}

这很好用,除了:

我。打字时渐变刷新引起的轻微闪烁

二。超过 TextView 底部的回车有时会导致渐变向上移位,从而导致 TextView 底部出现空白

iii.在输入超过边界并向下滚动到 TextView 边界之外后,当向上滚动到 TextView 顶部时,渐变仍然固定在 contentsize area 的底部;显然滚动不会触发 textViewDidChange() 委托(delegate)方法。

最佳答案

这里有一个解决方法:让 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
}
}

关于ios - 向可滚动的 UITextView 添加渐变背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51909352/

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