26

contentOffset が Swift で 2 つのポイント (下の図を参照) の間にある場合、スクロールビューの contentOffset をプログラムで設定したいと思います。

問題は、移動にスムーズなトランジションを追加したいのですが、これに関するドキュメントが見つかりませんでした。コンテンツ オフセットを徐々に減らすためにループを実行しようとしましたが、結果はあまり良くありません。

この例では、スクロールの最後でコンテンツ オフセットが 150 ピクセル未満の場合、オフセットが 100 に等しいポイントまでスムーズに移動します (アニメーションの持続時間は 1 秒になります)。150 ピクセルまでは移動します。 200pxまで。

何をすべきかの指示(ドキュメントまたは簡単な例)を提供していただければ、それは素晴らしいことです:)ありがとうございます!

ここに画像の説明を入力

4

4 に答える 4

40

使用できますUIView.animations

  func goToPoint() {
    dispatch_async(dispatch_get_main_queue()) {
      UIView.animateWithDuration(2, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
        self.scrollView.contentOffset.x = 200
        }, completion: nil)
    }
  }

迅速なバージョン

DispatchQueue.main.async {
    UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
        self.myScrollView.contentOffset.x = CGFloat(startingPointForView)
    }, completion: nil)
}

スイフト4

DispatchQueue.main.async {
    UIView.animate(withDuration: 1, delay: 0, options: UIView.AnimationOptions.curveLinear, animations: {
            self.scrollView.contentOffset.x = 200
    }, completion: nil)
}
于 2016-01-20T09:45:28.413 に答える
10

これがfatihyildizhanのコードのSwift 3バージョンです。

DispatchQueue.main.async {
    UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
        self.myScrollView.contentOffset.x = CGFloat(startingPointForView)
    }, completion: nil)
}
于 2016-10-03T15:55:36.257 に答える
3

スウィフト 4:

DispatchQueue.main.async {
    UIView.animate(withDuration: 1, delay: 0, options: UIView.AnimationOptions.curveLinear, animations: {
            self.scrollView.contentOffset.x = 200
    }, completion: nil)
}
于 2019-02-18T19:08:58.080 に答える