@@ -5,7 +5,8 @@ import React, { Component, PropTypes } from 'react';
55import {
66 View ,
77 Text ,
8- PanResponder
8+ PanResponder ,
9+ Animated
910} from 'react-native' ;
1011import { 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 }
0 commit comments