Skip to content

Commit bfaf3ce

Browse files
committed
ios default tab bar native animation
1 parent 9275905 commit bfaf3ce

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

DefaultTabBar.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,29 @@ const DefaultTabBar = React.createClass({
6565
bottom: 0,
6666
};
6767

68-
const left = this.props.scrollValue.interpolate({
69-
inputRange: [0, 1, ], outputRange: [0, containerWidth / numberOfTabs, ],
68+
const translateX = this.props.scrollX.interpolate({
69+
inputRange: [0, containerWidth * numberOfTabs],
70+
outputRange: [0, containerWidth],
7071
});
72+
7173
return (
7274
<View style={[styles.tabs, {backgroundColor: this.props.backgroundColor, }, this.props.style, ]}>
7375
{this.props.tabs.map((name, page) => {
7476
const isTabActive = this.props.activeTab === page;
7577
const renderTab = this.props.renderTab || this.renderTab;
7678
return renderTab(name, page, isTabActive, this.props.goToPage);
7779
})}
78-
<Animated.View style={[tabUnderlineStyle, { left, }, this.props.underlineStyle, ]} />
80+
<Animated.View
81+
style={[
82+
tabUnderlineStyle,
83+
{
84+
transform: [
85+
{ translateX },
86+
]
87+
},
88+
this.props.underlineStyle,
89+
]}
90+
/>
7991
</View>
8092
);
8193
},

index.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ const ScrollableTabView = React.createClass({
5858
},
5959

6060
getInitialState() {
61+
const width = Dimensions.get('window').width;
62+
const initialOffset = width * this.props.initialPage;
6163
return {
6264
currentPage: this.props.initialPage,
6365
scrollValue: new Animated.Value(this.props.initialPage),
64-
containerWidth: Dimensions.get('window').width,
66+
scrollX: new Animated.Value(initialOffset),
67+
containerWidth: width,
6568
sceneKeys: this.newSceneKeys({ currentPage: this.props.initialPage, }),
6669
};
6770
},
@@ -80,7 +83,7 @@ const ScrollableTabView = React.createClass({
8083
if (Platform.OS === 'ios') {
8184
const offset = pageNumber * this.state.containerWidth;
8285
if (this.scrollView) {
83-
this.scrollView.scrollTo({x: offset, y: 0, animated: !this.props.scrollWithoutAnimation, });
86+
this.scrollView.getNode().scrollTo({x: offset, y: 0, animated: !this.props.scrollWithoutAnimation, });
8487
}
8588
} else {
8689
if (this.scrollView) {
@@ -142,21 +145,33 @@ const ScrollableTabView = React.createClass({
142145

143146
renderScrollableContent() {
144147
if (Platform.OS === 'ios') {
148+
//onScroll={(e) => {
149+
//const offsetX = e.nativeEvent.contentOffset.x;
150+
//if (offsetX === 0 && !this.scrollOnMountCalled) {
151+
//this.scrollOnMountCalled = true;
152+
//} else {
153+
//this._updateScrollValue(offsetX / this.state.containerWidth);
154+
//}
155+
//}}
156+
157+
//onPageScroll={(e) => {
158+
//const { offset, position, } = e.nativeEvent;
159+
//this._updateScrollValue(position + offset);
160+
//}}
145161
const scenes = this._composeScenes();
146-
return <ScrollView
162+
return <Animated.ScrollView
147163
horizontal
148164
pagingEnabled
149165
automaticallyAdjustContentInsets={false}
150166
contentOffset={{ x: this.props.initialPage * this.state.containerWidth, }}
151167
ref={(scrollView) => { this.scrollView = scrollView; }}
152-
onScroll={(e) => {
153-
const offsetX = e.nativeEvent.contentOffset.x;
154-
if (offsetX === 0 && !this.scrollOnMountCalled) {
155-
this.scrollOnMountCalled = true;
156-
} else {
157-
this._updateScrollValue(offsetX / this.state.containerWidth);
158-
}
159-
}}
168+
onScroll={
169+
Animated.event([{
170+
nativeEvent: { contentOffset: { x: this.state.scrollX } }
171+
}], {
172+
useNativeDriver: true,
173+
})
174+
}
160175
onMomentumScrollBegin={this._onMomentumScrollBeginAndEnd}
161176
onMomentumScrollEnd={this._onMomentumScrollBeginAndEnd}
162177
scrollEventThrottle={16}
@@ -169,7 +184,7 @@ const ScrollableTabView = React.createClass({
169184
{...this.props.contentProps}
170185
>
171186
{scenes}
172-
</ScrollView>;
187+
</Animated.ScrollView>;
173188
} else {
174189
const scenes = this._composeScenes();
175190
return <ViewPagerAndroid
@@ -179,10 +194,13 @@ const ScrollableTabView = React.createClass({
179194
onPageSelected={this._updateSelectedPage}
180195
keyboardDismissMode="on-drag"
181196
scrollEnabled={!this.props.locked}
182-
onPageScroll={(e) => {
183-
const { offset, position, } = e.nativeEvent;
184-
this._updateScrollValue(position + offset);
185-
}}
197+
onPageScroll={
198+
Animated.event([{
199+
nativeEvent: { contentOffset: { x: this.state.scrollX } }
200+
}], {
201+
useNativeDriver: true,
202+
})
203+
}
186204
ref={(scrollView) => { this.scrollView = scrollView; }}
187205
{...this.props.contentProps}
188206
>
@@ -261,6 +279,7 @@ const ScrollableTabView = React.createClass({
261279
activeTab: this.state.currentPage,
262280
scrollValue: this.state.scrollValue,
263281
containerWidth: this.state.containerWidth,
282+
scrollX: this.state.scrollX,
264283
};
265284

266285
if (this.props.tabBarBackgroundColor) {

0 commit comments

Comments
 (0)