forked from Jiar/SegementSlide
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSegementSlideViewController+scroll.swift
96 lines (91 loc) · 3.75 KB
/
SegementSlideViewController+scroll.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// SegementSlideViewController+scroll.swift
// SegementSlide
//
// Created by Jiar on 2019/1/16.
// Copyright © 2019 Jiar. All rights reserved.
//
import UIKit
extension SegementSlideViewController {
internal func parentScrollViewDidScroll(_ parentScrollView: UIScrollView) {
defer {
scrollViewDidScroll(parentScrollView, isParent: true)
}
let parentContentOffsetY = parentScrollView.contentOffset.y
switch innerBouncesType {
case .parent:
if !canParentViewScroll {
parentScrollView.contentOffset.y = headerStickyHeight
canChildViewScroll = true
} else if parentContentOffsetY >= headerStickyHeight {
parentScrollView.contentOffset.y = headerStickyHeight
canParentViewScroll = false
canChildViewScroll = true
} else {
resetOtherCachedChildViewControllerContentOffsetY()
}
case .child:
let childBouncesTranslationY = -parentScrollView.panGestureRecognizer.translation(in: parentScrollView).y.rounded(.up)
defer {
lastChildBouncesTranslationY = childBouncesTranslationY
}
if !canParentViewScroll {
parentScrollView.contentOffset.y = headerStickyHeight
canChildViewScroll = true
} else if parentContentOffsetY >= headerStickyHeight {
parentScrollView.contentOffset.y = headerStickyHeight
canParentViewScroll = false
canChildViewScroll = true
} else if parentContentOffsetY <= 0 {
parentScrollView.contentOffset.y = 0
canChildViewScroll = true
resetOtherCachedChildViewControllerContentOffsetY()
} else {
guard let childScrollView = currentSegementSlideContentViewController?.scrollView else {
resetOtherCachedChildViewControllerContentOffsetY()
return
}
if childScrollView.contentOffset.y < 0 {
if childBouncesTranslationY > lastChildBouncesTranslationY {
scrollView.contentOffset.y = 0
canChildViewScroll = true
} else {
canChildViewScroll = false
}
} else {
canChildViewScroll = false
}
resetOtherCachedChildViewControllerContentOffsetY()
}
}
}
internal func childScrollViewDidScroll(_ childScrollView: UIScrollView) {
defer {
scrollViewDidScroll(childScrollView, isParent: false)
}
let parentContentOffsetY = scrollView.contentOffset.y
let childContentOffsetY = childScrollView.contentOffset.y
switch innerBouncesType {
case .parent:
if !canChildViewScroll, childScrollView.contentOffset.y != childContentOffsetY {
childScrollView.contentOffset.y = 0
} else if childContentOffsetY <= 0 {
canChildViewScroll = false
canParentViewScroll = true
}
case .child:
if !canChildViewScroll, childScrollView.contentOffset.y != childContentOffsetY {
childScrollView.contentOffset.y = 0
} else if childContentOffsetY <= 0 {
if parentContentOffsetY <= 0 {
canChildViewScroll = true
}
canParentViewScroll = true
} else {
if parentContentOffsetY > 0 && parentContentOffsetY < headerStickyHeight {
canChildViewScroll = false
}
}
}
}
}