标签: react-native-reanimated-v2

无法定义调用“worklet”的函数:“未定义不是函数”

我正在学习 React Native Reanimated 版本 2,但在创建调用'worklet'.

我在使用npx react-native init myApp.
我已遵循所有安装说明,如下所示。

  • 配置babel.config.js
    module.exports = {
      presets: ['module:metro-react-native-babel-preset'],
      plugins: [
        'react-native-reanimated/plugin'
      ]
    };
Run Code Online (Sandbox Code Playgroud)
  • 启用Hermes并将其设置为 true。
  • 配置MainApplication.java文件。
  • 清理构建。
  • 使用重置缓存yarn start --reset-cache

我尝试制作一个简单的Worklet函数,如下所示:

    import React from 'react';
    import { View, Button } from 'react-native';

    const App = () => {

      const someWorklet = () => {
        'worklet';
        console.log('this run on UI thread');
      };

      return (
        <View >
          <Button …
Run Code Online (Sandbox Code Playgroud)

javascript react-native react-native-reanimated react-native-reanimated-v2

4
推荐指数
1
解决办法
6682
查看次数

React-native-reanimated 2:如何更新文本以响应 PanGestureHandler?

这是我的组件。很简单,PanGestureHandler有一些 svg。

import React from 'react'
import { StyleSheet, View, Text } from 'react-native'
import Svg, { Circle, Line } from 'react-native-svg'
import Layout from '../constants/Layout'
import {
  PanGestureHandler,
  TapGestureHandler
} from 'react-native-gesture-handler'
import Animated, {
  useSharedValue,
  useAnimatedGestureHandler,
  useAnimatedProps,
  withTiming,
  Easing
} from 'react-native-reanimated'

const AnimatedCircle = Animated.createAnimatedComponent(Circle)

const chartWidth = Layout.window.width
const chartHeight = chartWidth / 2.6
const timing = 200

export default function Chart () {
  const x = useSharedValue(chartWidth / 2)
  const y = useSharedValue(chartHeight …
Run Code Online (Sandbox Code Playgroud)

react-native react-native-reanimated react-native-gesture-handler react-native-reanimated-v2

4
推荐指数
1
解决办法
5923
查看次数

如何在reanimated 2中使用diffClamp?

我试图根据来自 reanimated 2 useAnimatedScrollHandler的滚动事件隐藏和显示标题,并且我需要使用diffClamp,因此每当用户向上滚动时,标题的显示时间应少于整个滚动事件contentOffset.y值,但问题是 diffClamp 是我认为来自reanimated v1 的,我需要使用 useAnimatedStyle 钩子才能在reanimated v2中对样式进行动画处理,最后它给出了一个错误。

有人可以帮忙吗?

react-native react-native-reanimated react-native-reanimated-v2

3
推荐指数
1
解决办法
3465
查看次数

Reanimated 中的错误“尝试从不同线程同步调用函数 res”

有一些手势处理程序可以在浏览器中正常工作,但我在 iOS 上的 useAnimatedGestureHandler 挂钩的 onEnd 回调中收到此错误。

这是与我尝试添加的手势相关的所有代码

    const headerHeight = useSharedValue(176)

    const outerStyle = useAnimatedStyle(() => ({
        minHeight: 176,
        maxHeight: 416,
        height: headerHeight.value,
        borderBottomLeftRadius: 20,
        borderBottomRightRadius: 20,
        position: 'relative',
        overflow: 'visible',
        zIndex: 502,
    }))

    const innerStyle = useAnimatedStyle(() => ({
        overflow: 'hidden',
        height: headerHeight.value,
        minHeight: 176,
        maxHeight: 416,
        borderBottomLeftRadius: 20,
        borderBottomRightRadius: 20,
    }))

    
    const resizeHeaderHeight = useAnimatedGestureHandler({
        onStart: () => {},
        onActive: (event) => {
            headerHeight.value = event.absoluteY
        },
        onEnd: () => {
            if(headerHeight.value < 305) {
                headerHeight.value  = …
Run Code Online (Sandbox Code Playgroud)

react-native react-native-reanimated react-native-gesture-handler react-native-reanimated-v2

3
推荐指数
1
解决办法
6949
查看次数


React Native 平移手势处理程序不起作用

我正在关注本教程:https://youtu.be/r5XXSb4yQes(代码: https: //github.com/wcandillon/can-it-be-done-in-react-native/tree/master/season3/src/ CoinbasePro)并按照他所说的做了,但我无法使洗涤器(水平和垂直线)工作。

如您所见,即使我按下/拖动,滑块/线也不会出现。

在此输入图像描述

PanGestureHandler 触发时预期的水平线和垂直线:

在此输入图像描述

下面是相关代码:

主屏幕:

import Animated, {
  add,
  diffClamp,
  eq,
  modulo,
  sub,
} from "react-native-reanimated";
import { useValues, onGestureEvent } from "react-native-redash/lib/module/v1";
import { PanGestureHandler, State } from "react-native-gesture-handler";
import Svg, { Line } from "react-native-svg";
import data from "../../utils/data";

const MainScreen = () => {
  
  const caliber = data.length > 0 ? width / data.length : 0;

  const [x, y, state] = useValues(0, 0, State.UNDETERMINED);
  const gestureHandler = onGestureEvent({ x, …
Run Code Online (Sandbox Code Playgroud)

javascript react-native react-native-reanimated react-native-gesture-handler react-native-reanimated-v2

3
推荐指数
1
解决办法
6733
查看次数

Reanimated 2 创建worklet失败,可能是你忘记添加Reanimated的babel插件了?

我尝试了所有以前的解决方案,但没有人解决我的问题,我试图解决它 2 天

这是我的 babel 文件

在此处输入图片说明

我的代码

import React, { useRef, useState } from 'react'
import { View, useWindowDimensions, Button } from 'react-native'
import Animated, { runOnUI } from 'react-native-reanimated';

export default function Login() {
    const { width, height } = useWindowDimensions();
    // const value = useSharedValue(0);
    function someWorklet(greeting: any) {
        'worklet';
        console.log("Hey I'm running on the UI thread");
    }

    return (
        <View style={{ flex: 1, justifyContent: 'flex-end', alignItems: 'center' }}>
            <Button title="click me" onPress={() => runOnUI(someWorklet)('Howdy')} />
        </View>
    ); …
Run Code Online (Sandbox Code Playgroud)

react-native-reanimated-v2

2
推荐指数
8
解决办法
3019
查看次数

React Native运行useState/force在worklet函数中重新渲染

我正在调用react-native-reanimateduseAnimatedScrollHandler中的钩子来处理. 这个钩子按预期工作,但我现在想禁用一个基于. 但是,当共享值更改时,屏幕不会重新渲染,因为状态不会更改,因此按钮的外观保持不变。 有没有办法在工作集内部强制重新渲染,或者是否可以用于从工作集内部强制重新渲染?onScrollAnimated.ScrollViewcurrentIndexsharedValue
useState

const scrollHandler = useAnimatedScrollHandler((event) => {
  translationX.value = event.contentOffset.x
  if (event.contentOffset.x < width * 0.5 && currentIndex.value != 0) {
    currentIndex.value = 0
  } else if (
    event.contentOffset.x > width * 0.5 &&
    event.contentOffset.x < width * 1.5 &&
    currentIndex.value != 1
  ) {
    currentIndex.value = 1
  } else if (event.contentOffset.x > width * 1.5 && currentIndex.value != 2) {
    currentIndex.value = 2
  }
})
Run Code Online (Sandbox Code Playgroud)
<FlatButton
  label="Next"
  disabled={ …
Run Code Online (Sandbox Code Playgroud)

react-native react-native-reanimated-v2

2
推荐指数
1
解决办法
1816
查看次数

Reanimated v2 - 同步共享值

问题

我正在尝试使用 Reanimated v2 创建此动画:

在此输入图像描述

这是当前的实现:

const opacity1 = useSharedValue(0);
const opacity2 = useSharedValue(0);
const opacity3 = useSharedValue(0);
useEffect(() => {
  const duration = 600;
  const timingOptions = { duration: duration };
  const pulse = withTiming(1, timingOptions);
  const repeated = withRepeat(pulse, -1, true);
  opacity1.value = repeated;
  // *
  opacity2.value = withDelay(duration / 2, repeated);
  // *
  opacity3.value = withDelay(duration, repeated);
}, []);
Run Code Online (Sandbox Code Playgroud)

问题是,在我标记代码的地方// *似乎存在一些随机延迟(可能是由于 JS 执行),导致三个点有时最终同步(具有相同的不透明度)。

我认为问题在于更新这 3 个值不是原子的,因此在这些分配之间可能发生任何事情。

解决这个问题并使这 3 个共享值彼此之间具有确定性延迟的最佳方法是什么?


我尝试了什么?

  • 我尝试使用 1useSharedValue和 2 …

javascript typescript react-native react-native-reanimated-v2

2
推荐指数
1
解决办法
2640
查看次数

React Native Gesture Handler / React Native 动画 2:尝试从不同线程同步调用函数 {_readOnlyError}

错误 我的手机出现错误

我正在学习 React Native 手势处理程序和 React Native Reanimated 从文档中。当我获取手势坐标并将其传递给 useShared 值 translateX 时,出现此错误。我在运行 android 8.0.0 的 Samsung Galaxy A5 2017 上运行我的应用程序。请解释你的答案。

应用程序.js

// import { StatusBar } from 'expo-status-bar';
import { useEffect } from 'react';
import { StyleSheet, Text, View,StatusBar,TouchableOpacity } from 'react-native';
import Animated, {useSharedValue, useAnimatedStyle, withTiming,withSpring,withRepeat} from 'react-native-reanimated'
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { NavigationContainer } from '@react-navigation/native';
import Header from './components/Header';
import Gesture from './screens/Gesture';
const SimpleAnimation=({navigation})=>{
  const SIZE=100.0
  const handleRotation=(progress)=>{
    "worklet";
    return `${progress.value*2*Math.PI}rad`;
  } …
Run Code Online (Sandbox Code Playgroud)

react-native react-native-reanimated react-native-gesture-handler react-native-reanimated-v2

2
推荐指数
1
解决办法
3843
查看次数