@@ -5,7 +5,8 @@ import React, { Component, PropTypes } from 'react';
5
5
import {
6
6
View ,
7
7
Text ,
8
- PanResponder
8
+ PanResponder ,
9
+ Animated
9
10
} from 'react-native' ;
10
11
import { rollStyles } from './style' ;
11
12
@@ -43,6 +44,8 @@ class Pickroll extends Component {
43
44
constructor ( props , context ) {
44
45
super ( props , context ) ;
45
46
this . state = this . _stateFromProps ( props ) ;
47
+ this . init = true ;
48
+ this . moveDy = 0 ;
46
49
}
47
50
48
51
/**
@@ -77,22 +80,13 @@ class Pickroll extends Component {
77
80
*/
78
81
_move ( dy ) {
79
82
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 ;
86
84
this . middle && this . middle . setNativeProps ( {
87
85
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
94
87
}
95
88
} ) ;
89
+ this . forceUpdate ( ) ;
96
90
}
97
91
98
92
/**
@@ -106,7 +100,7 @@ class Pickroll extends Component {
106
100
let diff = _index - index ;
107
101
let marginValue ;
108
102
if ( diff && ! this . isMoving ) {
109
- marginValue = diff * 40 ;
103
+ marginValue = diff * 36 ;
110
104
this . _move ( marginValue ) ;
111
105
this . index = index ;
112
106
this . _onValueChange ( ) ;
@@ -127,9 +121,9 @@ class Pickroll extends Component {
127
121
}
128
122
129
123
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 ) ;
131
125
} 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 ) ;
133
127
}
134
128
}
135
129
@@ -140,9 +134,15 @@ class Pickroll extends Component {
140
134
* @private
141
135
*/
142
136
_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 ;
146
146
this . _onValueChange ( ) ;
147
147
}
148
148
@@ -151,46 +151,41 @@ class Pickroll extends Component {
151
151
*/
152
152
componentWillMount ( ) {
153
153
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 ) ,
156
157
onPanResponderRelease : this . _handlePanResponderRelease . bind ( this ) ,
157
158
onPanResponderMove : this . _handlePanResponderMove . bind ( this )
158
159
} ) ;
159
160
this . isMoving = false ;
160
161
this . index = this . state . selectedIndex ;
161
162
}
162
163
164
+ _handleStartShouldSetPanResponder ( e , gestureState ) {
165
+ return true ;
166
+ }
167
+
168
+ _handlePanResponderGrant ( ) {
169
+ console . debug ( 'Start to move' ) ;
170
+ }
163
171
/**
164
172
* 对单轮的每个格子进行初始化
165
173
* @param items {array} 需要渲染的总的数据
166
174
* @returns {{upItems: Array, middleItems: Array, downItems: Array} }
167
175
* @private
168
176
*/
169
- _renderItems ( items ) {
177
+ _renderItems ( items , init ) {
170
178
let upItems = [ ] , middleItems = [ ] , downItems = [ ] ;
171
179
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 ) ;
180
181
middleItems [ index ] = < Text
181
182
key = { 'mid' + index }
182
183
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 }
191
185
</ Text > ;
192
186
} ) ;
193
- return { upItems, middleItems, downItems } ;
187
+
188
+ return middleItems ;
194
189
}
195
190
196
191
/**
@@ -209,40 +204,24 @@ class Pickroll extends Component {
209
204
* @returns {XML }
210
205
*/
211
206
render ( ) {
207
+
212
208
let index = this . state . selectedIndex ;
213
209
let length = this . state . items . length ;
214
- let items = this . _renderItems ( this . state . items ) ;
215
210
216
- let upViewStyle = {
217
- marginTop : ( 3 - index ) * 30 ,
218
- height : length * 30
219
- } ;
211
+ this . init = false ;
220
212
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
226
215
} ;
227
216
228
217
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 } >
237
221
< 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 ) }
244
223
</ View >
245
- </ View >
224
+ </ View >
246
225
</ View >
247
226
) ;
248
227
}
0 commit comments