Files
react-native/packages/rn-tester/js/examples/Animated/TransformBounceExample.js
T
Luna Wei 63e0f7d767 Add useNativeDriver toggle UI
Summary:
Changelog: [Internal] - Update Animation examples to toggle useNativeDriver

There are issues when we toggle the native driver on and off. Where once off, the animations don't seem to update anymore, until we turn nativeDriver back on. See videos in testplan

Reviewed By: yungsters

Differential Revision: D29774939

fbshipit-source-id: cf6d70c1b574a87bb803510196dfb273b36de5e1
2021-07-22 17:24:02 -07:00

124 lines
3.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import * as React from 'react';
import RNTesterButton from '../../components/RNTesterButton';
import {Text, StyleSheet, View, Animated} from 'react-native';
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import RNTConfigurationBlock from '../../components/RNTConfigurationBlock';
import ToggleNativeDriver from './utils/ToggleNativeDriver';
const styles = StyleSheet.create({
content: {
backgroundColor: 'deepskyblue',
borderWidth: 1,
borderColor: 'dodgerblue',
padding: 20,
margin: 20,
borderRadius: 10,
alignItems: 'center',
},
});
function TransformBounceView({useNativeDriver}: {useNativeDriver: boolean}) {
const anim = new Animated.Value(0);
const bounceAnimation = Animated.spring(anim, {
// Returns to the start
toValue: 0,
// Velocity makes it move
velocity: 3,
// Slow
tension: -10,
// Oscillate a lot
friction: 1,
useNativeDriver,
});
return (
<>
<RNTesterButton
onPress={() => {
bounceAnimation.start();
}}>
Press to Fling it!
</RNTesterButton>
<Animated.View
style={[
styles.content,
{
transform: [
// Array order matters
{
scale: anim.interpolate({
inputRange: [0, 1],
outputRange: ([1, 4]: $ReadOnlyArray<number>),
}),
},
{
translateX: anim.interpolate({
inputRange: [0, 1],
outputRange: ([0, 500]: $ReadOnlyArray<number>),
}),
},
{
rotate: anim.interpolate({
inputRange: [0, 1],
outputRange: ([
'0deg',
'360deg', // 'deg' or 'rad'
]: $ReadOnlyArray<string>),
}),
},
],
},
]}>
<Text>Transforms!</Text>
</Animated.View>
</>
);
}
function TransformBounceExample(): React.Node {
const [useNativeDriver, setUseNativeDriver] = React.useState(false);
return (
<View>
<RNTConfigurationBlock>
<ToggleNativeDriver
value={useNativeDriver}
onValueChange={setUseNativeDriver}
/>
</RNTConfigurationBlock>
<TransformBounceView
key={`transform-bounce-view-${
useNativeDriver ? 'native' : 'js'
}-driver`}
useNativeDriver={useNativeDriver}
/>
</View>
);
}
export default ({
title: 'Transform Bounce',
name: 'transformBounce',
expect: 'Transform animation on rotation, translation, scale of View',
description: ('One `Animated.Value` is driven by a ' +
'spring with custom constants and mapped to an ' +
'ordered set of transforms. Each transform has ' +
'an interpolation to convert the value into the ' +
'right range and units.': string),
render: () => <TransformBounceExample />,
}: RNTesterModuleExample);