ScrollViewをカチッと止めてみた

# 普通のScrollViewがスーッと右から左へと過ぎ去っちゃってたけど，ページング画面にすることで綺麗になったよ．

```scrollDelegate.swift
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

        printHeader(fileNameStr: #file, funcNameStr: #function)
        let index = Int((scrollView.contentOffset.x / scrollView.frame.width).rounded())
        let fixX = CGFloat(index) * scrollView.frame.width
        self.calendarScrollV.setContentOffset(CGPoint(x:fixX, y:0), animated: false)
        let pos:CGFloat  = scrollView.contentOffset.x / scrollView.bounds.size.width
        let deff:CGFloat = pos - 1.0
        if abs(deff) >= 1.0 {
            if (deff > 0) {
                print("pre")
                self.components.month = self.components.month! + 1
                let monthAndYear = dateManager.decideMonthAndYear(year: self.components.year!, month: self.components.month!)
                self.components.year = monthAndYear.year
                self.components.month = monthAndYear.month
                self.lastCalendarCollection.reloadData()
                self.calendarCollection.reloadData()
                self.nextCalendarCollection.reloadData()
                self.updateHeaderTitleLabel(components: self.components)
                resetContentOffSet()
            } else {
                print("last")
                self.components.month = self.components.month! - 1
                let monthAndYear = dateManager.decideMonthAndYear(year: self.components.year!, month: self.components.month!)
                self.components.year = monthAndYear.year
                self.components.month = monthAndYear.month
                self.lastCalendarCollection.reloadData()
                self.calendarCollection.reloadData()
                self.nextCalendarCollection.reloadData()
                self.updateHeaderTitleLabel(components: self.components)
                resetContentOffSet()
            }
        }
    }
    
    func resetContentOffSet () {
        printHeader(fileNameStr: #file, funcNameStr: #function)
        lastCalendarCollection.frame = CGRect(x: 0, y: 0, width: calendarScrollV.frame.size.width, height: calendarScrollV.frame.size.height)
        calendarCollection.frame = CGRect(x: calendarScrollV.frame.size.width, y: 0, width: calendarScrollV.frame.size.width, height: calendarScrollV.frame.size.height)
        nextCalendarCollection.frame = CGRect(x: calendarScrollV.frame.size.width * 2.0, y: 0, width: calendarScrollV.frame.size.width, height: calendarScrollV.frame.size.height)
        
        let scrollViewDelegate:UIScrollViewDelegate = calendarScrollV.delegate!
        calendarScrollV.delegate = nil
        calendarScrollV.contentOffset = CGPoint(x: calendarScrollV.frame.size.width , y: 0.0);
        calendarScrollV.delegate = scrollViewDelegate
    }

```

scrollView内のoffsetの設定はこんな感じ．

```mainVC.swift

 override func viewDidLoad() {
        super.viewDidLoad()
       
        self.calendarCollection.frame.size = CGSize(width: self.calendarScrollV.frame.width, height: self.calendarScrollV.frame.height)
        self.calendarCollection.frame.origin = CGPoint(x: self.calendarCollection.frame.width, y: 0)
        self.nextCalendarCollection.frame.size = self.calendarCollection.frame.size
        self.lastCalendarCollection.frame.size = self.calendarCollection.frame.size
        self.nextCalendarCollection.frame.origin = CGPoint(x: self.calendarCollection.frame.width * 2, y: 0)
        self.lastCalendarCollection.frame.origin = CGPoint(x: 0, y: 0)
        
        self.calendarScrollV.contentSize = CGSize(width:self.calendarCollection.frame.width * 3, height:self.calendarCollection.frame.height)
        self.scrollContentV.frame = CGRect(x: 0, y: 0, width:self.calendarCollection.frame.width * 3, height:self.calendarCollection.frame.height)
        self.calendarScrollV.delegate = self
        self.calendarScrollV.contentOffset = CGPoint(x: self.calendarCollection.frame.width, y: 0)
    }
```

何してるかというと，

- ScrollView内に3つのCollectionView(contentsA, B, C)を配置します．
- contentsA, B, Cのframe.heightが高さ，frame.widthの合計値が横幅のcontentOffsetを用意します．
- 綺麗に配置します．
- ３枚の順番は左からABC
- 初期の表示画面をBにします．　

```swift
self.calendarScrollV.contentOffset = CGPoint(x: self.calendarCollection.frame.width, y: 0)
```

- 以下のらへんで表示画面の半分より右か左かでoffsetを対応する方向にずらして，collectionViewもずらす
 
```swift
        let pos:CGFloat  = scrollView.contentOffset.x / scrollView.bounds.size.width
        let deff:CGFloat = pos - 1.0
        if abs(deff) >= 1.0 {
            if (deff > 0) {
                print("pre")
                self.components.month = self.components.month! + 1
                let monthAndYear = dateManager.decideMonthAndYear(year: self.components.year!, month: self.components.month!)
                self.components.year = monthAndYear.year
                self.components.month = monthAndYear.month
                self.lastCalendarCollection.reloadData()
                self.calendarCollection.reloadData()
                self.nextCalendarCollection.reloadData()
                self.updateHeaderTitleLabel(components: self.components)
                resetContentOffSet()
            } else {
                print("last")
                self.components.month = self.components.month! - 1
                let monthAndYear = dateManager.decideMonthAndYear(year: self.components.year!, month: self.components.month!)
                self.components.year = monthAndYear.year
                self.components.month = monthAndYear.month
                self.lastCalendarCollection.reloadData()
                self.calendarCollection.reloadData()
                self.nextCalendarCollection.reloadData()
                self.updateHeaderTitleLabel(components: self.components)
                resetContentOffSet()
            }
        }
```

こんな感じのことをもっと綺麗にまとめているのは以下のサイトです．
> https://i-app-tec.com/ios/uiscrollview_page_scroll.html