使用弹簧动画动画化scrollRangeToVisible

问题描述 投票:0回答:1

我正在尝试动画滚动。在下面的代码中,detailText是一个UITextView。不知道为什么UIView.animate功能不起作用。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    UIView.animate(withDuration: 1.0, delay: 0.0,
                               usingSpringWithDamping: 0.6,
                               initialSpringVelocity: 6.0,
                               options: [.curveEaseIn], animations: {

  let rangeOfTag = (self.detailText.text as NSString).range(of: "satisfactorily")

  self.detailText.scrollRangeToVisible(rangeOfTag)                                                                                          

    }, completion: nil)

我尝试了上述内容:

override func viewDidLayoutSubviews()

没有成功创造弹簧。有什么想法吗?

swift animation
1个回答
0
投票

您可以使用此片段来获得振动。

   UIView.animate(withDuration: 3.0, delay: 0.0,
                   usingSpringWithDamping: 0.1,
                   initialSpringVelocity: 0.9,
                   options: [.curveEaseIn, .overrideInheritedCurve], animations: {

                   let rangeOfTag = (self.detailText.text as NSString)
                    .range(of: "satisfactorily")
                    let glyRange =    self.detailText.layoutManager.glyphRange(forCharacterRange: rangeOfTag, actualCharacterRange: nil)
                    let rect =     self.detailText.layoutManager.lineFragmentRect(forGlyphAt: NSMaxRange(glyRange), effectiveRange: nil)
                self.detailText.scrollRectToVisible(rect, animated: false)



    }, completion: nil)
© www.soinside.com 2019 - 2024. All rights reserved.