-
Notifications
You must be signed in to change notification settings - Fork 618
/
UIScrollView+Layout.swift
executable file
·82 lines (76 loc) · 3.16 KB
/
UIScrollView+Layout.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
// Copyright © 2017 Schibsted. All rights reserved.
import Foundation
extension UIScrollView {
override open class var expressionTypes: [String: RuntimeType] {
var types = super.expressionTypes
types["contentInsetAdjustmentBehavior"] = .uiScrollViewContentInsetAdjustmentBehavior
types["indicatorStyle"] = .uiScrollViewIndicatorStyle
types["indexDisplayMode"] = .uiScrollViewIndexDisplayMode
types["keyboardDismissMode"] = .uiScrollViewKeyboardDismissMode
types["zoomScale"] = .cgFloat
types["maximumZoomScale"] = .cgFloat
types["minimumZoomScale"] = .cgFloat
types["decelerationRate"] = .uiScrollView_DecelerationRate
#if arch(i386) || arch(x86_64)
// Private properties
for name in [
"accessoryViews",
"allowsMultipleFingers",
"autoscrollContentOffset",
"horizontalScrollDecelerationFactor",
"isProgrammaticScrollEnabled",
"preservesCenterDuringRotation",
"showBackgroundShadow",
"topExtensionViewColor",
"tracksImmediatelyWhileDecelerating",
"updateInsetBottomDuringKeyboardDismiss",
"verticalScrollDecelerationFactor",
] {
types[name] = nil
for key in types.keys where key.hasPrefix(name) {
types[key] = nil
}
}
#endif
return types
}
override open func setAnimatedValue(_ value: Any, forExpression name: String) throws {
switch name {
case "contentOffset":
setContentOffset(value as! CGPoint, animated: true)
case "contentOffset.x":
let offset = CGPoint(x: value as! CGFloat, y: contentOffset.y)
setContentOffset(offset, animated: true)
case "contentOffset.y":
let offset = CGPoint(x: contentOffset.x, y: value as! CGFloat)
setContentOffset(offset, animated: true)
case "zoomScale":
setZoomScale(value as! CGFloat, animated: true)
default:
try super.setValue(value, forExpression: name)
}
}
override open func setValue(_ value: Any, forExpression name: String) throws {
switch name {
case "contentInsetAdjustmentBehavior":
// Does nothing on iOS 10 and earlier
if #available(iOS 11.0, *) {
fallthrough
}
default:
try super.setValue(value, forExpression: name)
}
}
override open func didUpdateLayout(for _: LayoutNode) {
// Prevents contentOffset glitch when rotating from portrait to landscape
// TODO: needs improvement - result can be off by one page sometimes
if isPagingEnabled {
let offset = CGPoint(
x: round(contentOffset.x / frame.size.width) * frame.width - contentInset.left,
y: round(contentOffset.y / frame.size.height) * frame.height - contentInset.top
)
guard !offset.x.isNaN, !offset.y.isNaN else { return }
contentOffset = offset
}
}
}