Skip to content

Commit fdca711

Browse files
committed
feat(basic picker): change the realize of wheel
1 parent a801fce commit fdca711

File tree

4 files changed

+60
-111
lines changed

4 files changed

+60
-111
lines changed

app/roll.js

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import React, { Component, PropTypes } from 'react';
55
import {
66
View,
77
Text,
8-
PanResponder
8+
PanResponder,
9+
Animated
910
} from 'react-native';
1011
import {rollStyles} from './style';
1112

@@ -43,6 +44,8 @@ class Pickroll extends Component {
4344
constructor(props, context){
4445
super(props, context);
4546
this.state = this._stateFromProps(props);
47+
this.init = true;
48+
this.moveDy = 0;
4649
}
4750

4851
/**
@@ -77,22 +80,13 @@ class Pickroll extends Component {
7780
*/
7881
_move(dy){
7982
let index = this.index;
80-
this.middleHeight = Math.abs(-index * 40 + dy);
81-
this.up && this.up.setNativeProps({
82-
style: {
83-
marginTop: (3 - index) * 30 + dy * 0.75
84-
}
85-
});
83+
this.moveDy = dy;
8684
this.middle && this.middle.setNativeProps({
8785
style: {
88-
marginTop: -index * 40 + dy
89-
}
90-
});
91-
this.down && this.down.setNativeProps({
92-
style: {
93-
marginTop: (-index - 1) * 30 + dy * 0.75
86+
top: - 36 * index + 72 + dy
9487
}
9588
});
89+
this.forceUpdate();
9690
}
9791

9892
/**
@@ -106,7 +100,7 @@ class Pickroll extends Component {
106100
let diff = _index - index;
107101
let marginValue;
108102
if (diff && !this.isMoving) {
109-
marginValue = diff * 40;
103+
marginValue = diff * 36;
110104
this._move(marginValue);
111105
this.index = index;
112106
this._onValueChange();
@@ -127,9 +121,9 @@ class Pickroll extends Component {
127121
}
128122

129123
if (dy > 0) {
130-
this._move(dy > this.index * 40 ? this.index * 40 : dy);
124+
this._move(dy > this.index * 36 ? this.index * 36 : dy);
131125
} else {
132-
this._move(dy < (this.index - this.state.items.length + 1) * 40 ? (this.index - this.state.items.length + 1) * 40 : dy);
126+
this._move(dy < (this.index - this.state.items.length + 1) * 36 ? (this.index - this.state.items.length + 1) * 36 : dy);
133127
}
134128
}
135129

@@ -140,9 +134,15 @@ class Pickroll extends Component {
140134
* @private
141135
*/
142136
_handlePanResponderRelease(evt, gestureState){
143-
let middleHeight = this.middleHeight;
144-
this.index = middleHeight % 40 >= 20 ? Math.ceil(middleHeight / 40) : Math.floor(middleHeight / 40);
145-
this._move(0);
137+
let diff;
138+
diff = Math.abs(this.moveDy) % 36 >= 18 ? Math.ceil(Math.abs(this.moveDy / 36)) : Math.floor(Math.abs(this.moveDy / 36));
139+
if (this.moveDy >= 0) {this.index = this.index - diff} else {this.index = this.index + diff;}
140+
this.middle && this.middle.setNativeProps({
141+
style: {
142+
top: - 36 * this.index + 72
143+
}
144+
});
145+
this.moveDy = 0;
146146
this._onValueChange();
147147
}
148148

@@ -151,46 +151,41 @@ class Pickroll extends Component {
151151
*/
152152
componentWillMount(){
153153
this._panResponder = PanResponder.create({
154-
onMoveShouldSetPanResponder: (evt, gestureState) => true,
155-
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
154+
onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder.bind(this),
155+
onMoveShouldSetPanResponder: this._handleStartShouldSetPanResponder.bind(this),
156+
onPanResponderGrant: this._handlePanResponderGrant.bind(this),
156157
onPanResponderRelease: this._handlePanResponderRelease.bind(this),
157158
onPanResponderMove: this._handlePanResponderMove.bind(this)
158159
});
159160
this.isMoving = false;
160161
this.index = this.state.selectedIndex;
161162
}
162163

164+
_handleStartShouldSetPanResponder(e, gestureState){
165+
return true;
166+
}
167+
168+
_handlePanResponderGrant(){
169+
console.debug('Start to move');
170+
}
163171
/**
164172
* 对单轮的每个格子进行初始化
165173
* @param items {array} 需要渲染的总的数据
166174
* @returns {{upItems: Array, middleItems: Array, downItems: Array}}
167175
* @private
168176
*/
169-
_renderItems(items){
177+
_renderItems(items, init){
170178
let upItems = [], middleItems = [], downItems = [];
171179
items.forEach((item, index) => {
172-
upItems[index] = <Text
173-
key={'up' + index}
174-
className={'up' + index}
175-
style={[rollStyles.upText, this.state.itemStyle]}
176-
onPress={() => {this._moveTo(index);}} >
177-
{item.label}
178-
</Text>;
179-
180+
console.debug((1- Math.abs((index - this.index)*36 + this.moveDy)/36 * 0.05) * 22);
180181
middleItems[index] = <Text
181182
key={'mid' + index}
182183
className={'mid' + index}
183-
style={[rollStyles.middleText, this.state.itemStyle]}>{item.label}
184-
</Text>;
185-
downItems[index] = <Text
186-
key={'down' + index}
187-
className={'down' + index}
188-
style={[rollStyles.downText, this.state.itemStyle]}
189-
onPress={() => {this._moveTo(index);}} >
190-
{item.label}
184+
style={[rollStyles.middleText, this.state.itemStyle, {fontSize: (1- Math.abs((index - this.index)*36 + this.moveDy)/36 * 0.07) * 22, opacity: 1- Math.abs((index - this.index)*36 + this.moveDy)/36 * 0.4}]}>{item.label}
191185
</Text>;
192186
});
193-
return { upItems, middleItems, downItems };
187+
188+
return middleItems;
194189
}
195190

196191
/**
@@ -209,40 +204,24 @@ class Pickroll extends Component {
209204
* @returns {XML}
210205
*/
211206
render(){
207+
212208
let index = this.state.selectedIndex;
213209
let length = this.state.items.length;
214-
let items = this._renderItems(this.state.items);
215210

216-
let upViewStyle = {
217-
marginTop: (3 - index) * 30,
218-
height: length * 30
219-
};
211+
this.init = false;
220212
let middleViewStyle = {
221-
marginTop: -index * 40
222-
};
223-
let downViewStyle = {
224-
marginTop: (-index - 1) * 30,
225-
height: length * 30
213+
position: 'absolute',
214+
top: - 36 * index + 72
226215
};
227216

228217
return (
229-
230-
<View style={[rollStyles.container, this.state.pickerStyle]} {...this._panResponder.panHandlers}>
231-
<View style={rollStyles.up}>
232-
<View style={[rollStyles.upView, upViewStyle]} ref={(up) => { this.up = up }} >
233-
{ items.upItems }
234-
</View>
235-
</View>
236-
<View style={rollStyles.middle}>
218+
<View style={[{flex: 1}]}>
219+
<View style={{position: 'absolute', width: 400, height: 46, borderTopWidth: 1, borderBottomWidth: 1, borderColor: '#ccc', marginTop: 63}}></View>
220+
<View style={[rollStyles.container, this.state.pickerStyle]} {...this._panResponder.panHandlers} >
237221
<View style={[rollStyles.middleView, middleViewStyle]} ref={(middle) => { this.middle = middle }} >
238-
{ items.middleItems }
239-
</View>
240-
</View>
241-
<View style={rollStyles.down}>
242-
<View style={[rollStyles.downView, downViewStyle]} ref={(down) => { this.down = down }} >
243-
{ items.downItems }
222+
{ this._renderItems(this.state.items) }
244223
</View>
245-
</View>
224+
</View>
246225
</View>
247226
);
248227
}

app/setup3.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
Animated,
1212
Platform,
1313
PickerIOS,
14+
StatusBar,
1415
Modal,
1516
Image,
1617
} from 'react-native';
@@ -86,6 +87,9 @@ class TMPicker extends Component {
8687
this.state.animatedHeight = new Animated.Value(height);
8788
}
8889

90+
componentDidMount() {
91+
this._setEventBegin();
92+
}
8993
/**
9094
* 状态初始化
9195
* @param props {object} 继承的属性
@@ -179,7 +183,7 @@ class TMPicker extends Component {
179183
this.setState({visible: visible});
180184
Animated.timing(
181185
this.state.animatedHeight,
182-
{toValue: height-250}
186+
{toValue: height-StatusBar.currentHeight-220}
183187
).start();
184188
}else{
185189
Animated.timing(
@@ -213,6 +217,7 @@ class TMPicker extends Component {
213217
* @returns {XML}
214218
*/
215219
render(){
220+
console.debug(StatusBar.currentHeight);
216221
return (
217222
<View style={styles.container}>
218223
<Modal

app/style.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ let styles = StyleSheet.create({
4747
},
4848
innerContainer: {
4949
position:'absolute',
50-
top: height-250,
51-
height:250,
50+
height:220,
5251
width:width,
5352
backgroundColor:'white',
5453
},
@@ -87,7 +86,9 @@ let rollStyles = StyleSheet.create({
8786
container: {
8887
flex:1,
8988
justifyContent: 'center',
90-
alignItems: 'center'
89+
alignItems: 'center',
90+
flexDirection: 'row',
91+
backgroundColor: 'rgba(255,255,255,0)'
9192
},
9293
up: {
9394
height: 90,
@@ -110,25 +111,23 @@ let rollStyles = StyleSheet.create({
110111
},
111112
middle: {
112113
alignSelf:'stretch',
113-
height: 40,
114-
overflow: 'hidden',
115114
borderColor: '#aaa',
116115
borderTopWidth: 1 / ratio,
117116
borderBottomWidth: 1 / ratio
118117
},
119118
middleView: {
120-
height: 40,
121119
justifyContent: 'flex-start',
122120
alignItems: 'center'
123121
},
124122
middleText: {
125-
paddingTop: 0,
126-
height: 40,
123+
height: 36,
124+
paddingTop: 8,
125+
paddingBottom: 8,
127126
color: '#000',
128-
fontSize: 28,
127+
fontSize: 20,
129128
paddingBottom: 0,
130129
marginTop: 0,
131-
marginBottom: 0
130+
marginBottom: 0,
132131
},
133132
down: {
134133
height: 90,

index.android.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -172,48 +172,14 @@ class TpickerEx extends Component {
172172
return (
173173
<View style={testStyle.container}>
174174
<Text style={{margin:10}}>{this.state.str}</Text>
175-
<Cpicker
176-
pickerNameStyle = {{color:'red'}}
177-
cancelBtnStyle = {{color:'blue'}}
178-
pickerName = {'just a test'}
179-
inputValue={'4 level picker'}
180-
level = {4}
181-
selectIndex = {[0,1,1,0]}
182-
data = {level4Data}
183-
onResult = {(str)=>{
184-
this.setState({str:str});
185-
}}
186-
/>
187-
<Cpicker
188-
inputValue={'3 level picker'}
189-
level = {3}
190-
selectIndex = {[0,1,0]}
191-
data = {level3Data}
192-
visible = {false}
193-
/>
194-
<Cpicker
195-
inputValue={'2 level picker'}
196-
level = {2}
197-
selectIndex = {[0,1]}
198-
data = {level2Data}
199-
visible = {false}
200-
/>
201-
<Test3
202-
inputValue ={'3 wheel picker'}
203-
inputStyle = {testStyle.textInput}
204-
confirmBtnText = {'confirm'}
205-
cancelBtnText = {'cancel'}
206-
data = {wheel3}
207-
selectIndex = {[0,2,1]}
208-
visible = {false}
209-
/>
175+
210176
<Test3
211177
inputValue ={'2 wheel picker'}
212178
inputStyle = {testStyle.textInput}
213179
confirmBtnText = {'confirm'}
214180
cancelBtnText = {'cancel'}
215181
data = {wheel2}
216-
selectIndex = {[0,2]}
182+
selectIndex = {[0,1]}
217183
onResult ={(str) => {this.setState({str1:str});}}
218184
visible = {false}
219185
/>

0 commit comments

Comments
 (0)