通过春季动画设置animate scrollRangeToVisible

时间:2019-03-23 20:53:23

标签: swift animation

我正在尝试制作滚动动画。在下面的代码中,detailTextUITextView。不知道为什么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()

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

1 个答案:

答案 0 :(得分: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)