react-native Animated简单动画制作

react-native Animated简单动画制作

1、代码
import React from "react";
import { StyleSheet, View, Animated, Image } from "react-native";
class LoopAnimate extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            spinVal: new Animated.Value(0),
            spinVal1: new Animated.Value(0),
        }
    }
    componentDidMount() {
        this.startAnimate();
    }
   //开启动画
   tartAnimate = () => {
        const animatedLoop = Animated.sequence([
            Animated.parallel([
                Animated.timing(this.state.spinVal, {
                    toValue: 1,
                    duration: 200,
                    delay: 200,
                    useNativeDriver: true
                }),
                Animated.timing(this.state.spinVal1, {
                    toValue: 1,
                    duration: 200,
                    delay: 200,
                    useNativeDriver: true
                })
            ]),
            Animated.parallel([
                Animated.timing(this.state.spinVal, {
                    toValue: 0,
                    duration: 200,
                    useNativeDriver: true
                }),
                Animated.timing(this.state.spinVal1, {
                    toValue: 0,
                    duration: 200,
                    useNativeDriver: true
                })
            ])
        ])
        Animated.loop(animatedLoop).start();
    }
     render() {
        const douone = require('../assets/images/dou1.png');
        const { spinVal, spinVal1} = this.state;
        const spinValue = spinVal.interpolate({ // 0、1值转化'20deg'、'0deg'
            inputRange: [0, 1],
            outputRange: ['20deg', '0deg']
        })
         const spinValue1 = spinVal1.interpolate({
            inputRange: [0, 1],
            outputRange: ['20deg', '0deg']
        })
        return (
            <View>
                <Animated.View style={{transform: [{rotate: spinValue}]}}>
                    <Image source={douone} style={styles.douImgcss}></Image>
                </Animated.View>
                <Animated.View style={{transform: [{rotate: spinValue1}]}}>
                    <Image source={douone} style={styles.douImgcss}></Image>
                </Animated.View>
            </View>
        )
    }
}
var styles = StyleSheet.create({
    douImgcss: {
        position: "absolute",
        width: pxToDp(60),
        height: pxToDp(73),
        top: pxToDp(-100),
        resizeMode: 'contain'
    }
})
export default LoopAnimate;
2、效果

[‘20deg’, ‘0deg’]来回循环摆动

3、animated功能介绍

一、 Animated.sequence循环执行
二、 Animated.parallel 同时开始一个动画数组里的全部动画
三、 Animated.timing 推动一个值按照一个过渡曲线而随时间变化
四、 interpolate转化值例:0、1值转化’20deg’、‘0deg’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值