gpt4 book ai didi

swift - 调整 View 大小时,textview 会抖动

转载 作者:行者123 更新时间:2023-11-30 12:47:00 24 4
gpt4 key购买 nike

我正在调整 TextView 所属的 View 的大小,当 View 变大或变小时,文本会晃动。

所述 TextView 的声明:

lazy var textview: UITextView = {
let textView = UITextView()
textView.text = ""
textView.font = .systemFont(ofSize: 12, weight: UIFontWeightMedium)
textView.isScrollEnabled = false
textView.isEditable = false
textView.isSelectable = true
textView.isUserInteractionEnabled = true
textView.translatesAutoresizingMaskIntoConstraints = false
textView.textAlignment = .center
textView.textColor = .lightGray
textView.dataDetectorTypes = .link
return textView
}()

我正在调整它所在的 View 大小以适合全屏,如下所示

if let window = UIApplication.shared.keyWindow  {
let statusBarHeight = UIApplication.shared.statusBarFrame.size.height

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveLinear, animations: {

self.frame = CGRect(x: 0, y: statusBarHeight, width: window.frame.width, height: window.frame.height - statusBarHeight)

self.layer.cornerRadius = 0

self.layoutIfNeeded()

}, completion: nil)
}

这样做后, View 完美展开,但 textviews 文本会产生反弹效果,使动画看起来非常不专业......有什么建议吗?

编辑:看起来当我删除中心文本对齐选项时它工作正常。如何使其在文本居中对齐的情况下工作?

最佳答案

编辑:我又看了一遍,并尝试使用基于 UIScrollView animation of height and contentOffset "jumps" content from bottom 的技术。 .

这是一个最小的工作示例,其中 TextView 具有居中文本对齐方式,这对我有用!

我建议管理动画要么全部基于约束,要么全部基于帧。我尝试了一个通过更新容器 View 框架来驱动动画的版本,但开始需要很长时间才能将其保留在这种基于约束的方法中。

希望这能为您指明正确的方向:)

import UIKit

class ViewController: UIViewController {

lazy var textView: UITextView = {
let textView = UITextView()
textView.text = "testing text view"
textView.textAlignment = .center
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()

lazy var containerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

var widthConstraint: NSLayoutConstraint!
var topAnchor: NSLayoutConstraint!

override func viewDidLoad() {

view.backgroundColor = .groupTableViewBackground

// add container view and constraints
view.addSubview(containerView)
containerView.frame = view.bounds.insetBy(dx: 100, dy: 200)
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 100).isActive = true

// keep reference to topAnchor and width as properties to animate
topAnchor = containerView.topAnchor.constraint(lessThanOrEqualTo: view.topAnchor, constant: 100)
widthConstraint = containerView.widthAnchor.constraint(equalToConstant: 300)
topAnchor.isActive = true
widthConstraint.isActive = true

// add text view to container view and set constraints
containerView.addSubview(textView)
textView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
textView.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
textView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
textView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
}

@IBAction func toggleResize(_ sender: UIButton) {

sender.isSelected = !sender.isSelected

view.layoutIfNeeded()
widthConstraint.constant = sender.isSelected ? view.bounds.width : 300
topAnchor.constant = sender.isSelected ? 20 : 100

// caculate the textView content offset for starting position based on
// expected end position at end of the animation
let xOffset = (textView.bounds.width - widthConstraint.constant) / 2
textView.contentOffset = CGPoint(x: -xOffset, y: textView.contentOffset.y)

UIView.animate(withDuration: 1) {
self.view.layoutIfNeeded()
}
}
}

关于swift - 调整 View 大小时,textview 会抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41523797/

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