使用动画滚动textView的底部

时间:2017-06-24 23:42:49

标签: ios swift animation uitextview

我的代码右侧动画向下滚动,但只是停在我放置y坐标的位置。我希望动画继续到textview的最底层。

     UIView.animate(withDuration: 9) { 


        self.textView.contentOffset = CGPoint(x: 0, y: 100)
    }

2 个答案:

答案 0 :(得分:0)

使用scrollRangeToVisible

游乐场示例:

import UIKit
import PlaygroundSupport

func scrollTextViewToBottom(_ textView: UITextView) {
    if(textView.text.characters.count > 0 ) {
        guard let text = textView.text else {
            return
        }
        let bottom = NSRange(location: text.characters.count - 1, length: 1)
        textView.scrollRangeToVisible(bottom)
    }
}

var textView = UITextView(frame: CGRect(x: 0, y: 0, width: 200, height: 300))
textView.text = "start long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts long texts end"

PlaygroundPage.current.liveView = textView

scrollTextViewToBottom(textView)

答案 1 :(得分:0)

有两种方法可以滚动到UIScrollView的底部(UITextViewUIScrollView的子类)。

手动设置内容偏移动画(允许您使用自定义动画):

let textView = UITextView()

UIView.animate(withDuration: 9) {
    let contentSize = textView.contentSize
    let contentInset = textView.contentInset
    let textViewBounds = textView.bounds
    let newContentOffset = CGPoint(
        x: contentSize.width + contentInset.right - textViewBounds.width,
        y: contentSize.height + contentInset.bottom - textViewBounds.height
    )
    textView.contentOffset = newContentOffset
}

或者使用scrollRectToVisible(_:animated:)方法(更简单但使用内置动画):

let textView = UITextView()

let contentSize = textView.contentSize
let contentInset = textView.contentInset
let bottomRightPosition = CGPoint(
    x: contentSize.width + contentInset.right,
    y: contentSize.height + contentInset.bottom
)
textView.scrollRectToVisible(CGRect(origin: bottomRightPosition, size: .zero), animated: true)

这些示例都会滚动,直到textView的右下角可见(textViews通常不会水平滚动,因此相当于滚动到底部)。