View
テキトーにScrollView
の中に画像とボタン一個入れておく
ScrollViewの実装方法はこちらを参考に
ViewController
ボタン一個だけ接続しておく。(普通のIBActionでもOK)
import UIKit
import RxSwift
import RxCocoa
class HomeViewController: UIViewController {
@IBOutlet private weak var backToTopButton: UIButton!
@IBOutlet private weak var scrollView: UIScrollView!
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
bind()
}
private func bind() {
backToTopButton.rx.tap.asSignal()
.emit(onNext: { [weak self] in
self?.scrollToTop()
})
.disposed(by: disposeBag)
}
private func scrollToTop() {
let desiredOffset = CGPoint(x: 0, y: -scrollView.contentInset.top)
scrollView.setContentOffset(desiredOffset, animated: true)
}
}