Skip to content

Commit 92f45dc

Browse files
authored
Merge pull request ptomasroos#571 from sangotaro/native-animation
Migrate to native animation
2 parents 423aabe + c400d29 commit 92f45dc

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

index.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ const ScrollableTabView = React.createClass({
5656
},
5757

5858
getInitialState() {
59+
const width = Dimensions.get('window').width;
5960
return {
6061
currentPage: this.props.initialPage,
62+
scrollX: new Animated.Value(this.props.initialPage * width),
6163
scrollValue: new Animated.Value(this.props.initialPage),
62-
containerWidth: Dimensions.get('window').width,
64+
containerWidth: width,
6365
sceneKeys: this.newSceneKeys({ currentPage: this.props.initialPage, }),
6466
};
6567
},
@@ -74,6 +76,12 @@ const ScrollableTabView = React.createClass({
7476
this.setTimeout(() => {
7577
InteractionManager.runAfterInteractions(scrollFn);
7678
}, 0);
79+
80+
this.state.scrollX.addListener(({ value, }) => {
81+
const scrollValue = value / this.state.containerWidth;
82+
this.state.scrollValue.setValue(scrollValue);
83+
this.props.onScroll(scrollValue);
84+
});
7785
},
7886

7987
componentWillReceiveProps(props) {
@@ -142,16 +150,19 @@ const ScrollableTabView = React.createClass({
142150

143151
renderScrollableContent() {
144152
const scenes = this._composeScenes();
145-
return <ScrollView
153+
return <Animated.ScrollView
146154
horizontal
147155
pagingEnabled
148156
automaticallyAdjustContentInsets={false}
149157
contentOffset={{ x: this.props.initialPage * this.state.containerWidth, }}
150158
ref={(scrollView) => { this.scrollView = scrollView; }}
151-
onScroll={(e) => {
152-
const offsetX = e.nativeEvent.contentOffset.x;
153-
this._updateScrollValue(offsetX / this.state.containerWidth);
154-
}}
159+
onScroll={
160+
Animated.event([{
161+
nativeEvent: { contentOffset: { x: this.state.scrollX } }
162+
}], {
163+
useNativeDriver: true,
164+
})
165+
}
155166
onMomentumScrollBegin={this._onMomentumScrollBeginAndEnd}
156167
onMomentumScrollEnd={this._onMomentumScrollBeginAndEnd}
157168
scrollEventThrottle={16}
@@ -164,7 +175,7 @@ const ScrollableTabView = React.createClass({
164175
{...this.props.contentProps}
165176
>
166177
{scenes}
167-
</ScrollView>;
178+
</Animated.ScrollView>;
168179
},
169180

170181
_composeScenes() {
@@ -209,11 +220,6 @@ const ScrollableTabView = React.createClass({
209220
});
210221
},
211222

212-
_updateScrollValue(value) {
213-
this.state.scrollValue.setValue(value);
214-
this.props.onScroll(value);
215-
},
216-
217223
_handleLayout(e) {
218224
const { width, } = e.nativeEvent.layout;
219225

@@ -235,6 +241,7 @@ const ScrollableTabView = React.createClass({
235241
goToPage: this.goToPage,
236242
tabs: this._children().map((child) => child.props.tabLabel),
237243
activeTab: this.state.currentPage,
244+
scrollX: this.state.scrollX,
238245
scrollValue: this.state.scrollValue,
239246
containerWidth: this.state.containerWidth,
240247
};

0 commit comments

Comments
 (0)