You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->
## Summary
This PR is to optimise using sensors.
Before:
Each `useAnimatedSensor` call creates a new native listener that update
shared value assigned to it.
Now:
We identify sensors listeners by sensor type and config properties and
the same configurations use the same shared value.
## Test plan
- Example app
```
import Animated, {
useAnimatedStyle,
useAnimatedSensor,
SensorType,
} from 'react-native-reanimated';
import { StyleSheet, View } from 'react-native';
import React from 'react';
export default function AnimatedSensorExample() {
const sensorObject = (color: string, sensorType: any) => {
const gravity = useAnimatedSensor(sensorType, { interval: 16 });
console.log(gravity.sensor.value);
const animatedStyle = useAnimatedStyle(() => {
//console.log(gravity.sensor.value);
return {
top: -gravity.sensor.value.y * 300,
left: gravity.sensor.value.x * 200,
};
});
return (
<View style={styles.container}>
<Animated.View style={[{...styles.box, backgroundColor: color}, animatedStyle]} />
</View>
)
};
return (
<View style={styles.container}>
{sensorObject('red', SensorType.GRAVITY)}
{sensorObject('blue', SensorType.ACCELEROMETER)}
{sensorObject('green', SensorType.GRAVITY)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
box: {
width: 150,
height: 150,
backgroundColor: 'navy',
},
});
```
0 commit comments