Skip to content

Commit 015d746

Browse files
author
amoreno
committed
Animations changes
1 parent 68d625c commit 015d746

File tree

5 files changed

+156
-89
lines changed

5 files changed

+156
-89
lines changed

.DS_Store

6 KB
Binary file not shown.

lib/IntroSlider.js

Lines changed: 92 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import React from 'react'
3+
import React from 'react';
44
import {
55
View,
66
StyleSheet,
@@ -9,15 +9,15 @@ import {
99
Animated,
1010
StatusBar,
1111
Dimensions
12-
} from 'react-native'
13-
import SkipButton from './template/SkipButton'
14-
import Animations from './animations/Animations'
15-
import { ifIphoneX } from 'react-native-iphone-x-helper'
12+
} from 'react-native';
13+
import SkipButton from './template/SkipButton';
14+
import Animations from './animations/Animations';
15+
import { ifIphoneX } from 'react-native-iphone-x-helper';
1616

17-
import type { Element } from 'react'
18-
import type { AnimationTypes } from './animations/Animations'
17+
import type { Element } from 'react';
18+
import type { AnimationTypes } from './animations/Animations';
1919

20-
const SCREEN_WIDTH = Dimensions.get('window').width
20+
const SCREEN_WIDTH = Dimensions.get('window').width;
2121

2222
type Props = {
2323
children: any,
@@ -29,99 +29,123 @@ type Props = {
2929
skipLastTitleButton?: string,
3030
skipButton?: Element<*>,
3131
customAnimation?: Object,
32-
animationType?: string
33-
}
32+
animationType?:
33+
| 'Parallax'
34+
| 'Pager'
35+
| 'ZoomOut'
36+
| 'FadeIn'
37+
| 'TranslateY'
38+
| 'RotateX'
39+
| 'RotateY'
40+
| 'Flip',
41+
showSkipButton?: boolean,
42+
tintStatusBar?: boolean
43+
};
3444
type State = {
3545
xOffset: Animated.Value,
3646
page: number
37-
}
47+
};
3848

3949
class IntroSlider extends React.Component<Props, State> {
40-
scrollView: ScrollView
50+
scrollView: ScrollView;
4151

4252
static defaultProps = {
4353
page: 0,
44-
animationType: 'Pager'
45-
}
54+
animationType: 'Pager',
55+
showSkipButton: true,
56+
tintStatusBar: true
57+
};
4658

4759
constructor(props: Props) {
48-
super(props)
60+
super(props);
4961
this.state = {
5062
xOffset: new Animated.Value(0),
5163
page: props.page
52-
}
64+
};
5365
}
5466

5567
componentWillMount() {
56-
const position: number = this.state.page
68+
const position: number = this.state.page;
5769
if (this.isAndroidStatusBar() && this.props.tintStatusBar) {
5870
StatusBar.setBackgroundColor(
5971
this.shadeStatusBar(
6072
this.props.children[position].props.backgroundColor,
6173
-0.3
6274
),
6375
true
64-
)
76+
);
6577
}
6678
}
6779

6880
renderDots = (): Element<*> => {
69-
const { activeDotStyle, inactiveDotStyle } = this.props
70-
let dotsArray: Array<*> = []
81+
const { activeDotStyle, inactiveDotStyle } = this.props;
82+
let dotsArray: Array<*> = [];
7183

7284
for (let index = 0; index < this.props.children.length; index++) {
73-
const isSelected: boolean = this.state.page === index
85+
const isSelected: boolean = this.state.page === index;
7486
dotsArray.push(
7587
<View
7688
key={index}
77-
style={[styles.dot, isSelected ? activeDotStyle : inactiveDotStyle]}
89+
style={[
90+
styles.dot,
91+
isSelected
92+
? {
93+
...styles.active,
94+
...activeDotStyle
95+
}
96+
: inactiveDotStyle
97+
]}
7898
/>
79-
)
99+
);
80100
}
81-
return <View style={styles.dotsContainer}>{dotsArray}</View>
82-
}
101+
return <View style={styles.dotsContainer}>{dotsArray}</View>;
102+
};
83103

84-
_getAnimation = (index: number, animationType: string): Function => {
85-
return Animations[animationType](index, this.state.xOffset)
86-
}
104+
_getAnimation = (index: number, animationType: string): ?Function => {
105+
if (!Animations[animationType]) {
106+
return;
107+
}
108+
return Animations[animationType](index, this.state.xOffset);
109+
};
87110

88111
/**
89112
* Render each child view in the ViewPager
90113
* @returns {*}
91114
*/
92115
renderChildren = (): Array<Element<*>> => {
93-
const { customAnimation } = this.props
94116
return this.props.children.map((child: Object, index: number) => {
95117
return (
96-
<View key={index} style={{ width: SCREEN_WIDTH }}>
97-
<Animated.View
98-
style={[
99-
styles.container,
100-
this._getAnimation(index, this.props.animationType) || customAnimation
101-
]}>
102-
{child}
103-
</Animated.View>
118+
<View
119+
key={index}
120+
style={{
121+
width: SCREEN_WIDTH
122+
}}>
123+
{React.cloneElement(child, {
124+
animation:
125+
this.props.customAnimation ||
126+
this._getAnimation(index, this.props.animationType)
127+
})}
104128
</View>
105-
)
106-
})
107-
}
129+
);
130+
});
131+
};
108132

109133
/**
110134
* Handles page change event
111135
* @param e
112136
*/
113137
onPageSelected = (e: Object) => {
114138
// Calculate current index
115-
const index = Math.round(e.nativeEvent.contentOffset.x / SCREEN_WIDTH)
139+
const index = Math.round(e.nativeEvent.contentOffset.x / SCREEN_WIDTH);
116140

117141
// Only call the function if the index is an integer
118142
if (index === parseInt(index, 10)) {
119143
if (index < 0 || index >= React.Children.count(this.props.children)) {
120-
return
144+
return;
121145
}
122-
this.handleBottomStepper(index)
146+
this.handleBottomStepper(index);
123147
}
124-
}
148+
};
125149

126150
/**
127151
* Handles bottom stepper buttons behaviour
@@ -135,18 +159,18 @@ class IntroSlider extends React.Component<Props, State> {
135159
-0.3
136160
),
137161
false
138-
)
162+
);
139163
}
140-
this.setState({ page: position })
141-
}
164+
this.setState({ page: position });
165+
};
142166

143167
shadeStatusBar = (color: string, percent: number): string => {
144168
const first = parseInt(color.slice(1), 16),
145169
black = first & 0x0000ff,
146170
green = (first >> 8) & 0x00ff,
147171
percentage = percent < 0 ? percent * -1 : percent,
148172
red = first >> 16,
149-
theme = percent < 0 ? 0 : 255
173+
theme = percent < 0 ? 0 : 255;
150174

151175
return (
152176
'#' +
@@ -158,27 +182,27 @@ class IntroSlider extends React.Component<Props, State> {
158182
)
159183
.toString(16)
160184
.slice(1)
161-
)
162-
}
185+
);
186+
};
163187

164188
isAndroidStatusBar = (): boolean => {
165-
return this.props.children.length > 0 && Platform.OS === 'android'
166-
}
189+
return this.props.children.length > 0 && Platform.OS === 'android';
190+
};
167191

168192
render() {
169-
const isLast: boolean = this.props.children.length - 1 === this.state.page
193+
const isLast: boolean = this.props.children.length - 1 === this.state.page;
170194
return (
171195
<View style={styles.container}>
172196
<Animated.ScrollView
173197
ref={(ref: any) => {
174-
this.scrollView = ref
198+
this.scrollView = ref;
175199
}}
176200
style={styles.container}
177201
horizontal={true}
178202
pagingEnabled={true}
179203
removeClippedSubviews={true}
180204
directionalLockEnabled={true}
181-
scrollEventThrottle={1}
205+
scrollEventThrottle={10}
182206
bounces={false}
183207
scrollsToTop={false}
184208
automaticallyAdjustContentInsets={false}
@@ -188,7 +212,9 @@ class IntroSlider extends React.Component<Props, State> {
188212
[
189213
{
190214
nativeEvent: {
191-
contentOffset: { x: this.state.xOffset }
215+
contentOffset: {
216+
x: this.state.xOffset
217+
}
192218
}
193219
}
194220
],
@@ -204,7 +230,9 @@ class IntroSlider extends React.Component<Props, State> {
204230
<SkipButton
205231
onSkip={this.props.onSkip}
206232
skipTitleButton={
207-
isLast ? this.props.skipLastTitleButton : this.props.skipTitleButton
233+
isLast
234+
? this.props.skipLastTitleButton
235+
: this.props.skipTitleButton
208236
}
209237
/>
210238
) : (
@@ -213,7 +241,7 @@ class IntroSlider extends React.Component<Props, State> {
213241
{this.renderDots()}
214242
</View>
215243
</View>
216-
)
244+
);
217245
}
218246
}
219247

@@ -223,15 +251,14 @@ const styles = StyleSheet.create({
223251
overflow: 'hidden'
224252
},
225253
bottom: {
226-
bottom: 12,
254+
bottom: 30,
227255
...ifIphoneX({
228256
bottom: 40
229257
})
230258
},
231259
dotsContainer: {
232260
flexDirection: 'row',
233261
justifyContent: 'center',
234-
bottom: 5,
235262
position: 'absolute',
236263
alignItems: 'center',
237264
justifyContent: 'center',
@@ -244,7 +271,10 @@ const styles = StyleSheet.create({
244271
borderRadius: 20,
245272
backgroundColor: 'rgba(0, 0, 0, .2)',
246273
marginHorizontal: 5
274+
},
275+
active: {
276+
backgroundColor: 'white'
247277
}
248-
})
278+
});
249279

250-
export default IntroSlider
280+
export default IntroSlider;

0 commit comments

Comments
 (0)