Skip to content

Commit f1db741

Browse files
committed
feat(basic picker): add click to item
1 parent fdca711 commit f1db741

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

app/roll.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ class Pickroll extends Component {
7373
};
7474
}
7575

76+
/**
77+
* 组件需要渲染前,对手势事件处理进行初始化
78+
*/
79+
componentWillMount(){
80+
this._panResponder = PanResponder.create({
81+
onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder.bind(this),
82+
onMoveShouldSetPanResponder: this._handleStartShouldSetPanResponder.bind(this),
83+
onPanResponderGrant: this._handlePanResponderGrant.bind(this),
84+
onPanResponderRelease: this._handlePanResponderRelease.bind(this),
85+
onPanResponderMove: this._handlePanResponderMove.bind(this)
86+
});
87+
this.isMoving = false;
88+
this.index = this.state.selectedIndex;
89+
}
90+
7691
/**
7792
* 根据移动的距离重新布局
7893
* @param dy {number} 移动的距离
@@ -103,6 +118,7 @@ class Pickroll extends Component {
103118
marginValue = diff * 36;
104119
this._move(marginValue);
105120
this.index = index;
121+
this.moveDy = 0;
106122
this._onValueChange();
107123
} else { return 'you are moving';}
108124
}
@@ -135,6 +151,7 @@ class Pickroll extends Component {
135151
*/
136152
_handlePanResponderRelease(evt, gestureState){
137153
let diff;
154+
console.debug(gestureState.vy);
138155
diff = Math.abs(this.moveDy) % 36 >= 18 ? Math.ceil(Math.abs(this.moveDy / 36)) : Math.floor(Math.abs(this.moveDy / 36));
139156
if (this.moveDy >= 0) {this.index = this.index - diff} else {this.index = this.index + diff;}
140157
this.middle && this.middle.setNativeProps({
@@ -146,20 +163,6 @@ class Pickroll extends Component {
146163
this._onValueChange();
147164
}
148165

149-
/**
150-
* 组件需要渲染前,对手势事件处理进行初始化
151-
*/
152-
componentWillMount(){
153-
this._panResponder = PanResponder.create({
154-
onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder.bind(this),
155-
onMoveShouldSetPanResponder: this._handleStartShouldSetPanResponder.bind(this),
156-
onPanResponderGrant: this._handlePanResponderGrant.bind(this),
157-
onPanResponderRelease: this._handlePanResponderRelease.bind(this),
158-
onPanResponderMove: this._handlePanResponderMove.bind(this)
159-
});
160-
this.isMoving = false;
161-
this.index = this.state.selectedIndex;
162-
}
163166

164167
_handleStartShouldSetPanResponder(e, gestureState){
165168
return true;
@@ -177,10 +180,10 @@ class Pickroll extends Component {
177180
_renderItems(items, init){
178181
let upItems = [], middleItems = [], downItems = [];
179182
items.forEach((item, index) => {
180-
console.debug((1- Math.abs((index - this.index)*36 + this.moveDy)/36 * 0.05) * 22);
181183
middleItems[index] = <Text
182184
key={'mid' + index}
183185
className={'mid' + index}
186+
onPress={() => {this._moveTo(index)}}
184187
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}
185188
</Text>;
186189
});

0 commit comments

Comments
 (0)