`contentOffset` does not execute when rect Y value of glyph is significantly distant from preceding rect

47 Views Asked by At

Anyone experience an issue with contentOffset when using it to scroll to a rect of a string in a UITextView but the view does not scroll at all? It is as if contentOffset does not know what to do with the rect passed to it. This error ocurrs only when there is significant vertical distance between two rect values relative to the contentsize.height of the UITextView. Otherwise, contentOffset works fine: it will scroll the view to each rect value passed to contentOffset. I am using layoutManager to get the rect of the glyph to pass into contentOffset.

The problem appears to be corrected if I first manually scroll to the bottom of the view and then execute contentoffset. I am not sure why first scrolling to the bottom of the UITextView corrects the error.

My original post did not include the code which is executed when pressing button. The function receives one NSRange at a time. The NSRange values are stored in an array and the button increments to each NSRange value and converts into a 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 )

}

0

There are 0 best solutions below