Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush UI operation queue when animation is not running #4329

Merged
merged 5 commits into from
Apr 5, 2023

Conversation

piaskowyk
Copy link
Member

@piaskowyk piaskowyk commented Apr 4, 2023

Summary

When animation is not running it is possible to enqueue UI update operation but never execute them.

Before After
Screen.Recording.2023-04-05.at.09.48.36.mov
Screen.Recording.2023-04-05.at.09.49.10.mov

I tested it on the Fabric also.

code
import Animated, {
  useSharedValue,
  useAnimatedStyle,
  useAnimatedProps,
  withTiming
} from 'react-native-reanimated';
import { View, Button } from 'react-native';
import React from 'react';

export default function AnimatedStyleUpdateExample(): React.ReactElement {
  const sv1 = useSharedValue(20);
  const sv2 = useSharedValue(20);
  const style1 = useAnimatedStyle(() => ({ width: sv1.value }));
  const style2 = useAnimatedStyle(() => ({ width: sv2.value }));

  return (
    <View
      style={{
        flex: 1,
        flexDirection: 'column',
        marginTop: 50
      }}>
      <Animated.View
        style={[
          { width: 100, height: 80, backgroundColor: 'orange'},
          style1,
        ]}
      />
      <Animated.View
        style={[
          { width: 100, height: 80, backgroundColor: 'black'},
          style2,
        ]}
      />
      <Button
        title={'set width for 1'}
        onPress={() => {
          sv1.value = withTiming(sv1.value === 20 ? 100 : 20);
        }}
      />
      <Button
        title={'set width for 2'}
        onPress={() => {
          sv2.value = sv2.value === 20 ? 100 : 20;
        }}
      />
      <Button
        title={'set width for 1 & 2'}
        onPress={() => {
          sv1.value = withTiming(sv1.value === 20 ? 100 : 20);
          sv2.value = sv2.value === 20 ? 100 : 20;
        }}
      />
    </View>
  );
}

@piaskowyk piaskowyk marked this pull request as ready for review April 5, 2023 08:12
ios/REANodesManager.h Show resolved Hide resolved
Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Co-authored-by: Tomek Zawadzki <tomasz.zawadzki@swmansion.com>
Copy link
Member

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm this PR successfully resolves the issue with useAnimatedSensor

@ranaavneet
Copy link
Sponsor

@piaskowyk @tomekzaw does this solve #4231 as well?

@tomekzaw
Copy link
Member

@ranaavneet unfortunately, probably not, but we're working on it

fluiddot pushed a commit to wordpress-mobile/react-native-reanimated that referenced this pull request Jun 5, 2023
…ion#4329)

## Summary

When animation is not running it is possible to enqueue UI update
operation but never execute them.

| Before      | After |
| ----------- | ----------- |
| <video
src="https://user-images.githubusercontent.com/36106620/230021472-c41aa77b-d955-4503-8273-a26e29dc7dea.mov"
/> | <video
src="https://user-images.githubusercontent.com/36106620/230021555-cbb62d7a-5f2e-4eb1-8d21-d3169aa1bfaf.mov"
/> |

I tested it on the Fabric also.

<details>
<summary>code</summary>

```js
import Animated, {
  useSharedValue,
  useAnimatedStyle,
  useAnimatedProps,
  withTiming
} from 'react-native-reanimated';
import { View, Button } from 'react-native';
import React from 'react';

export default function AnimatedStyleUpdateExample(): React.ReactElement {
  const sv1 = useSharedValue(20);
  const sv2 = useSharedValue(20);
  const style1 = useAnimatedStyle(() => ({ width: sv1.value }));
  const style2 = useAnimatedStyle(() => ({ width: sv2.value }));

  return (
    <View
      style={{
        flex: 1,
        flexDirection: 'column',
        marginTop: 50
      }}>
      <Animated.View
        style={[
          { width: 100, height: 80, backgroundColor: 'orange'},
          style1,
        ]}
      />
      <Animated.View
        style={[
          { width: 100, height: 80, backgroundColor: 'black'},
          style2,
        ]}
      />
      <Button
        title={'set width for 1'}
        onPress={() => {
          sv1.value = withTiming(sv1.value === 20 ? 100 : 20);
        }}
      />
      <Button
        title={'set width for 2'}
        onPress={() => {
          sv2.value = sv2.value === 20 ? 100 : 20;
        }}
      />
      <Button
        title={'set width for 1 & 2'}
        onPress={() => {
          sv1.value = withTiming(sv1.value === 20 ? 100 : 20);
          sv2.value = sv2.value === 20 ? 100 : 20;
        }}
      />
    </View>
  );
}

```

</details>

---------

Co-authored-by: Tomek Zawadzki <tomasz.zawadzki@swmansion.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants