文档开发者中心
我的
|

HarmonyOS 5

HarmonyOS RN动画卡顿

收起
自动换行
深色代码主题
复制
import React, { Component } from "react";
import { Animated, FlatList, Text, TouchableOpacity, View } from "react-native";
let maxY = 500;
let minY = 200;
class DemoPageAnimate extends Component {
  DATA = [];
  constructor(props) {
    super(props);
    this.top = maxY;
    this.topValue = new Animated.Value(this.top);
    for (let i = 0; i < 50; i++) {
      this.DATA.push({ title: "title : " + i });
    }
  }
  scrollAnimToBotton() {
    console.log("scrollAnimToBotton" + this.top + "xxxx" + maxY);
    // this.topValue.setValue(this.top);
    this.refs.topView && this.refs.topView.setNativeProps({
      style: {
        display: "flex",
      },
    });
    Animated.spring(this.topValue, {
      toValue: maxY, duration: 300, bounciness: 15, speed: 20, useNativeDriver: false,
    }).start(() => {
      console.log("=======start");
      this.top = maxY;
    });
  }
  scrollAnimToTOP() {
    console.log("scrollAnimToTOP" + this.top + "xxxx" + minY);
    this.refs.topView && this.refs.topView.setNativeProps({
      style: {
        display: "none",
      },
    });
    Animated.timing(this.topValue, {
      toValue: minY, duration: 200, useNativeDriver: false,
    }).start(() => {
      this.top = minY;
    });
  }
  render() {
    return <View style={{ width: "100%", height: "100%", backgroundColor: "#f00" }}>
      <TouchableOpacity style={{
        width: "100%", height: "10%", backgroundColor: "#fff", justifyContent: "center", alignItems: "center",
        marginTop: 10,
      }}
                        onPress={() => this.scrollAnimToTOP()}>
        <Text>弹出</Text>
      </TouchableOpacity>
      <TouchableOpacity style={{
        width: "100%", height: "10%", backgroundColor: "#fff", justifyContent: "center", alignItems: "center",
        marginTop: 10,
      }}
                        onPress={() => this.scrollAnimToBotton()}>
        <Text>弹下</Text>
      </TouchableOpacity>
      <Animated.View
        ref="scrollView"
        style={{
          zIndex: 2, position: "absolute", width: "100%", backgroundColor: "#0f0",
          top: this.topValue, height: 800,
        }}
      >
        <Text>AAAAAAA<View style={{ backgroundColor: "#00f" }}><Text>BBBBB</Text></View></Text>
        <FlatList
          style={{ width: "100%", height: 300 }}
          data={this.DATA}
          renderItem={(item) => {
            return (<Text style={{ height: 50, width: "100%" }}>{item.item.title}
              {/*加上这个View,动画会非常卡顿*/}
              <View><Text>-BBBBB</Text></View>
            </Text>);
          }}
          keyExtractor={item => item.id}
        />
      </Animated.View>
    </View>;
  };
}
module.exports = DemoPageAnimate;

当FlatList中的<Text>嵌套<View>时,动画会明显卡顿。

点赞
收藏
回复
1
分享
举报
浏览132 发布于2025-04-23 08:30未知归属地
全部评论
最多点赞
最新发布
最早发布
|
技术支持

可以通过将createAndRegisterRNInstance的enableBackgroundExecutor设置成false来解决这个问题。

1楼回复于2025-04-23 08:30 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。