Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React Native Animations

React Native Animations

In this talk we will discuss how to implement animations in React Native by comparing them to CSS animations

Emmett Harper

April 26, 2017
Tweet

Other Decks in Education

Transcript

  1. Step 1 Define initial animatable property .slideIn { transform: translate(0px,

    0px); transition-property: transform; transition-duration: 2s; transition-timing-function: ease-in; transition-delay: 1s; } CSS
  2. Step 2 Set Animatable Style Property on Component <html> <body>

    <div class='slide-in-box'> My Sliding Box </div> </body> </html> html
  3. Step 3 Start the animation window.onload(() => { const box

    = document .getElementsByClassName( 'slideIn').item(0); box.style.transform = 'translate(200px, 0px)'; }); es6
  4. Step 1 Define initial animatable property const { Component }

    = require('react'); const { Animated, Easing } = require('react-native'); class SlidingBox extends Component { constructor(props) { super(props); this.slideIn = Animated.timing( this.state.slide, { toValue: { x: 200, y: 0 }, duration: 2000, delay: 1000, easing: Easing.in(Easing.ease) } ) this.state = { slide: new Animated.ValueXY({ x: 0, y: 0 }); } } } JSX A. Import Animated library B. Create an animation C. Create an animating state variable
  5. Step 2 Set Animatable Style Property on Component class SlidingBox

    extends Component { … render() { const slideStyle = this.state.slide.getTranslateTransform(); return ( <Animated.View style={slideStyle}> <Text>My Sliding Box</Text> </Animated.View> ); } } JSX getTranslateTransform(): returns the tweening state variable Animated.View is one of 3 default animatable components Others are Animated.Text, Animated.Image Any component can be an animatable component with: const AnimatedScrollView = Animated .createAnimatedComponent(ScrollView)
  6. Step 3 Start the animation class SlidingBox extends Component {

    … componentDidMount() { this.slideIn.start(); } } JSX
  7. Timing Functions Timing Animated.timing( this.state.slide, { toValue: number, duration: number,

    delay: number, easing: Easing } ) Spring Decay Animated.spring( this.state.slide, { toValue: number overshootClamping: bool restDisplacementThreshold: number restSpeedThreshold: number velocity: number bounciness: number speed: number tension: number friction: number } Animated.decay( this.state.slide, { toValue: number Deceleration: number } ) JSX
  8. Composing Functions Animated.sequence([ Animated.timing(this.animVal, { toValue: 100, duration: 200 }),

    Animated.timing(this.animVal, { toValue: 0, duration: 200 }) ]).start() [ Sequence ] Parallel Stagger Delay JSX
  9. Composing Functions Animated.parallel([ Animated.timing(this.animVal, { toValue: 100, duration: 200 }),

    Animated.timing(this.animVal, { toValue: 0, duration: 200 }) ]).start() Sequence [ Parallel ] Stagger Delay JSX
  10. Composing Functions Animated.stagger(100, [ Animated.timing(this.animVal, { toValue: 100, duration: 200

    }), Animated.timing(this.animVal, { toValue: 0, duration: 200 }) ]).start() Sequence Parallel [ Stagger ] Delay JSX 100ms 100ms