Кто-нибудь испытывает проблему с contentOffset
при использовании его для прокрутки до rect
строки в UITextView
, но представление вообще не прокручивается? Как будто contentOffset
не знает, что делать с rect
, переданным ему. Эта ошибка возникает только , когда имеется значительное вертикальное расстояние между двумя значениями rect
относительно contentsize.height
из UITextView
. В противном случае contentOffset
работает нормально: он будет прокручивать представление для каждого значения rect
, переданного в contentOffset
. Я использую layoutManager
, чтобы заставить rect
глифа перейти в contentOffset
.
Проблема, по-видимому, устраняется, если сначала я прокручиваю вручную до нижней части представления, а затем выполняю contentoffset
. Я не уверен, почему первая прокрутка вниз до UITextView
исправляет ошибку.
Мой оригинальный пост не содержал код, который выполняется при нажатии кнопки. Функция получает один NSRange за раз. Значения NSRange сохраняются в массиве, и кнопка увеличивается до каждого значения NSRange и преобразуется в rect
.
// called when button pressed.
func matchProcessing(matchRange:NSRange, InTextView textView:UITextView){
// unhide the rounded rectangle view that will circle matched text
self.detailText.viewWithTag(1)?.isHidden = false
//get rect of NSRange of matched text
let matchTxtRect = textView.layoutManager.boundingRect(forGlyphRange: matchRange, in: textView.textContainer)
//animation of scroll and rounded rectangle view
UIView.animate(withDuration: 0.8, delay: 0.0, options: .curveEaseInOut, animations:
{
textView.contentOffset = CGPoint(x: 0.0, y: matchTxt.origin.y)
// get rect of
let nextDestination = self.frameOfTextInRange(range: self.arrayOfMatches[self.matchIndex], inTextView: textView)
//this does not work either: let nextDestination = textView.layoutManager.boundingRect(forGlyphRange: self.arrayOfMatches[self.matchIndex], in: textView.textContainer)
let destination = textView.viewWithTag(1)?.convert(textView.viewWithTag(1)!.center, from: textView.viewWithTag(1)?.superview)
textView.viewWithTag(1)?.move(to: (destination?.applying(
CGAffineTransform(translationX: nextDestination.origin.x, y: nextDestination.origin.y)))!,
duration: 0.8,
options: .curveEaseInOut)
UIView.animate(withDuration: 0.6, delay:0, options: [.repeat, .autoreverse, .curveEaseInOut], animations: {
self.detailText.viewWithTag(1)?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
})
}, completion: nil )
}