调整视图大小时,textview会抖动

时间:2017-01-07 16:40:17

标签: swift uitextview

我正在调整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
    }()

我正在调整视图,以便像这样适合全屏

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)
}

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

编辑:似乎当我删除中心文本对齐选项时,它工作正常。如何使文本中心对齐?

1 个答案:

答案 0 :(得分:0)

编辑:我再看一下这个并尝试使用UIScrollView animation of height and contentOffset "jumps" content from bottom中的技术。

这是一个最小的工作示例,文本视图中心文本对齐,这对我有用!

我建议管理动画要么全部基于约束,要么基于所有帧。我尝试了一个通过更新容器视图框架来驱动动画的版本,但是在这种基于约束的方法中,它开始花费太长时间才能保留它。

希望这能指出你正确的方向:)

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()
        }
    }
}