微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当引脚不正确时反应原生摇动引脚视图

如何解决当引脚不正确时反应原生摇动引脚视图

我正在尝试实现一个功能,当用户输入错误的密码时,4 个小圆圈会左右摇晃。

我创建了一个动画组件来测试我的代码并且它可以工作,但现在我的问题是我如何仅在密码不正确时应用它?

目前,当我输入错误的密码时,没有任何反应。我期望的是视图水平移动。


const shake = new Animated.Value(0.5);

const [subtle,setSubtle]=useState(true);
const [auto,setAuto]=useState(false);

const translateXAnim = shake.interpolate({
  inputRange: [0,1],outputRange: [subtle ? -8 : -16,subtle ? 8 : 16],});

const getAnimationStyles = () => ({
  transform: [
    {
      translateX: translateXAnim,},],});

const runAnimation = () => {
  Animated.sequence([
    Animated.timing(shake,{
      delay: 300,tovalue: 1,duration: subtle ? 300 : 200,easing: Easing.out(Easing.sin),useNativeDriver: true,}),Animated.timing(shake,{
      tovalue: 0,duration: subtle ? 200 : 100,{
      tovalue: 1,{
      tovalue: 0.5,]).start(() => {
    if (auto) runAnimation();
  });
};

const stopAnimation = () => {
 shake.stopAnimation();
};

const handleConfirm = async()=>{  
      const result = await authApi();
  
      if(!result.ok) {
      setAuto(true)
      setSubtle(true)
      runAnimation()
      stopAnimation()

      return setLoginFailed(true);
      }
  
      setLoginFailed(false);
      };

return(

<Animated.View style={[getAnimationStyles()]}>

        <View style={styles.circleBlock}> 
         {
           password.map(p=>{
             let style =p != ''?styles.circleFill
               : styles.circle
            
            return <View style={style}></View>
           })
         }
          </View>

          </Animated.View>

解决方法

问题是您没有将 useRef() 用于

const shake = new Animated.Value(0.5);

应该

const shake = useRef(new Animated.Value(0.5)).current;

useRef 返回一个可变的 ref 对象,其 .current 属性是 初始化为传递的参数 (initialValue)。返回的对象 将在组件的整个生命周期内持续存在。

https://reactjs.org/docs/hooks-reference.html#useref

enter image description here

为动画抖动效果制作了具有相同输入和输出范围值的单独博览会小吃。看看吧。

博览会:https://snack.expo.io/@klakshman318/runshakeanimation

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?