-
Notifications
You must be signed in to change notification settings - Fork 30
/
AwareCusKeyBoardScrollView.js
164 lines (134 loc) · 4.74 KB
/
AwareCusKeyBoardScrollView.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//@flow
import React, {Component, PureComponent} from 'react'
import {
ScrollView,
findNodeHandle,
Dimensions,
TextInput,
View,
Platform,
Keyboard,
UIManager,
} from 'react-native'
import * as CustomKeyboard from './customKeyboard'
export default class AwareCusKeyBoardScrollView extends PureComponent {
state: Object
//防止自定义键盘切换之间的闪跳
showKeyBoard: boolean
resetTimeout: number
//监听系统键盘
showKeyBoardSub: any
hideKeyboardSub: any
//监听自定义键盘
showCustomKeyBoardSub: any
hideCustomKeyboardSub: any
//区分showSys->hideSys和showSys->showCus->hideSys之间的区别
flag: number
constructor() {
super(...arguments)
this.flag = 0
this.state = {
showKeyBoard: false,
}
}
componentDidMount() {
if (Platform.OS === 'android') {
this.showCustomKeyBoardSub = CustomKeyboard.addKeyBoardShowListener(this._onFocus)
this.hideCustomKeyboardSub = CustomKeyboard.addKeyBoardHideListener(this._onReset)
this.showKeyBoardSub = Keyboard.addListener('keyboardDidShow', this._showSysKeyborad)
this.hideKeyboardSub = Keyboard.addListener('keyboardDidHide', this._hideSysKeyboard)
}
}
componentDidUpdate(preProps: Object, preState: Object,) {
if (preState.showKeyBoard != this.state.showKeyBoard) {
this._updateScrollTo()
}
}
componentWillUnmount() {
this.showCustomKeyBoardSub && CustomKeyboard.removeKeyBoardListener(this.showCustomKeyBoardSub)
this.hideCustomKeyboardSub && CustomKeyboard.removeKeyBoardListener(this.hideCustomKeyboardSub)
this.showKeyBoardSub && this.showKeyBoardSub.remove()
this.hideKeyboardSub && this.hideKeyboardSub.remove()
this.scrollTimeout && clearTimeout(this.scrollTimeout)
this.resetTimeout && clearTimeout(this.resetTimeout)
}
_showSysKeyborad = (frames: Object) => {
console.log('show system keyboard')
//防止自定义键盘跳到系统键盘出bug
this.resetTimeout && clearTimeout(this.resetTimeout)
this.flag++
}
_hideSysKeyboard = () => {
this.flag--
if(this.flag === 0) {
//之间切换状态到键盘不显示
this.showKeyBoard = false
this._changeKeyBoardState()
}
}
_onFocus = () => {
this.showKeyBoard = true
this.flag += 2
this.resetTimeout && clearTimeout(this.resetTimeout)
this._changeKeyBoardState()
}
_onReset = () => {
this.showKeyBoard = false
this.flag -= 2
this.resetTimeout && clearTimeout(this.resetTimeout)
this.resetTimeout = setTimeout(()=>{
this._changeKeyBoardState()
}, 200)
}
_changeKeyBoardState = () => {
this.setState((preState) => {
if (preState.showKeyBoard === this.showKeyBoard) {
this._updateScrollTo()
return preState
}
return {showKeyBoard: this.showKeyBoard}
})
}
_onError = () => {}
_updateScrollTo = () => {
if(TextInput.State.currentlyFocusedField() == null) {
return
}
const currentlyTfNode = TextInput.State.currentlyFocusedField()
const scrollViewNode = findNodeHandle(this.refs.scrollView)
//显示和隐藏键盘
if (this.state.showKeyBoard) {
UIManager.measureInWindow(scrollViewNode, (x, y, width, height) => {
UIManager.measureLayout(
currentlyTfNode,
scrollViewNode,
this._onError,
(left, top, width, height) => {
const windowHeight = Dimensions.get('window').height
const subHeight = windowHeight - CustomKeyboard.currentHeight
const currentHeight = top + height + y + 30 //上下padding高度
if (subHeight < currentHeight) {
this.refs.scrollView.scrollTo({y: currentHeight - subHeight})
}
}
)
})
} else {
this.refs.scrollView.scrollTo({y: 0})
}
}
render() {
const {children, otherProps} = this.props
return (
<ScrollView
ref="scrollView"
key="scrollView"
keyboardShouldPersistTaps="handled"
{...otherProps}
>
{this.props.children}
<View style={{height: this.state.showKeyBoard ? CustomKeyboard.currentHeight : 0}}/>
</ScrollView>
)
}
}