代码之家  ›  专栏  ›  技术社区  ›  Somename

无法在React Native中执行变换动画

  •  1
  • Somename  · 技术社区  · 8 年前

    无法获取 Animated 工作。我正在添加按钮样式和 Animated.View 在容器中。所以当我 pressIn 我想要 有生气的看法 缩小到0.5,启用时 pressOut 希望它返回到1。我设置了 this.animatedValue = new Animated.Value(1) 在…上 componentWillMount 它本身我的代码如下:

    'use strict';
    
    import React, { Component } from 'react';
    import { StyleSheet, Text, Animated, Easing, Image,
             TouchableWithoutFeedback, View} from 'react-native';
    
    class MyAnim extends Component {
        constructor(props) {
          super(props);
          this.state = {};
          this.handlePressIn = this.handlePressIn.bind(this);
          this.handlePressOut = this.handlePressOut.bind(this);
        }
    
      componentWillMount() {
        this.animatedValue = new Animated.Value(1);
      }
    
      handlePressIn() {
        Animated.spring(this.animatedValue,{
            to: .5
        }).start()
      }
      handlePressOut() {
        Animated.spring(this.animatedValue,{
            to: 1,
            friction: 3,
            tension: 40
        }).start()
      }
    
      render() {
        const animatedStyle = {
            transform: [{ scale: this.animatedValue }]
        }
        return (
          <View style={styles.container}>
            <TouchableWithoutFeedback
              onPressIn={this.handlePressIn}
              onPressOut={this.handlePressOut}
            >
              <Animated.View style={[styles.button, animatedStyle]}>
                <Text style={styles.buttonText}>Press Me</Text>
              </Animated.View>
            </TouchableWithoutFeedback>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container:{
        flex:1, justifyContent:'center', alignItems:'center'
      },
      button:{
        backgroundColor:'#333', width:100, height:50, marginBottom:10,
        justifyContent:'center', alignItems:'center'
      },
      buttonText:{
        color:'#fff'
      },
    });
    
    export default MyAnim;
    

    我正在尝试一个简单的春季动画。错误是:

    enter image description here

    我做错了什么?我希望按钮转换为大小。按入时为5,按出时为1。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Pritish Vaidya    8 年前

    您需要使用 toValue 而不是 to 如最新 docs

    跟踪速度状态以创建流体运动作为 toValue公司 更新,并可以链接在一起。

    也如他们的 SpringAnimation.js 配置

     toValue: number | AnimatedValue | {x: number, y: number} | AnimatedValueXY,
    
    handlePressIn() {
            Animated.spring(this.animatedValue,{
                toValue: .5
            }).start()
        }
    
    handlePressOut() {
        Animated.spring(this.animatedValue,{
            toValue: 1,
            friction: 3,
            tension: 40
        }).start()
      }
    
    推荐文章