记得第一次使用到 css 的 animation 跟 transform 系列效果
做了很多厉害的动画东西飞来飞去转来转去
这次我们来使用 React Native animated 来做简单的动画吧!
经过前两篇的介绍,大致简单的使用都了解了吧~所以我们就直接开始噜!
import { Animated } from 'react-native';
<View>
//球 A 将 leftValue 放入 translateX
<Animated.View
style={{
margin: 15,
width: 50,
height: 50,
borderRadius: 100 / 2,
backgroundColor: 'red',
transform: [{translateX: leftValue}],
}}
/>
//球 B将 rightValue 放入 translateX
<Animated.View
style={{
position: 'absolute',
margin: 15,
width: 50,
height: 50,
borderRadius: 100 / 2,
backgroundColor: 'red',
transform: [{translateX: rightValue}],
}}
/>
// 叫球A出去 func moveBall
<TouchableOpacity onPress={moveBall}>
<Text style={styles.btnText}>Move Me</Text>
</TouchableOpacity>
// 叫球B进来 func backBall
<TouchableOpacity onPress={backBall}>
<Text style={styles.btnText}>Back Me</Text>
</TouchableOpacity>
</View>
const leftValue = useRef(new Animated.Value(0)).current;
const rightValue = useRef(new Animated.Value(-500)).current;
// B 球 Value - 500 到 0
const backBall = () => {
Animated.timing(rightValue, {
toValue: 0,
duration: 1000,
useNativeDriver: true,
}).start();
};
// A 球 Value 0 到 500
const moveBall = () => {
Animated.timing(leftValue, {
toValue: 500,
duration: 1000,
useNativeDriver: true,
}).start();
};
import React, {useRef} from 'react';
import {Animated, View, StyleSheet, TouchableOpacity, Text} from 'react-native';
const AnimatedTranslate = props => {
const leftValue = useRef(new Animated.Value(0)).current;
const rightValue = useRef(new Animated.Value(-500)).current;
const backBall = () => {
Animated.timing(rightValue, {
toValue: 0,
duration: 1000,
useNativeDriver: true,
}).start();
};
const moveBall = () => {
Animated.timing(leftValue, {
toValue: 500,
duration: 1000,
useNativeDriver: true,
}).start();
};
return (
<View>
<Animated.View
style={{
margin: 15,
width: 50,
height: 50,
borderRadius: 100 / 2,
backgroundColor: 'red',
transform: [{translateX: leftValue}],
}}
/>
<Animated.View
style={{
position: 'absolute',
margin: 15,
width: 50,
height: 50,
borderRadius: 100 / 2,
backgroundColor: 'red',
transform: [{translateX: rightValue}],
}}
/>
<TouchableOpacity onPress={moveBall}>
<Text style={styles.btnText}>Move Me</Text>
</TouchableOpacity>
<TouchableOpacity onPress={backBall}>
<Text style={styles.btnText}>Back Me</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
btnText: {
textAlign: 'center',
backgroundColor: '#aaa',
marginVertical: 10,
},
});
export default AnimatedTranslate;
按 Move Me 球球滚出去 (下图)
按 Back Me 球球滚回来(下图)
Day 18 done!! 请多多指教
>>: [前端暴龙机,Vue2.x 进化 Vue3 ] Day24.Vue3 Options API & Composition API 介绍
序言 此系列文章将不定期更新,献给未来健忘的自己,也献给刚接触 Laravel 的初学者们,若您在观...
Angular [目标] 启动程序先导入Login元件 1. 新增元件、模组 Syntax:ng h...
目的 取得一段讯息後,解析、转译成具有特定含义的讯息。 说明 简单来说,就是讯息的转换器。 当有分析...
挑战目标: MockNative Camp 最近不止工作还有很多事情要处理,今天只能先暂停一次。 之...
大家好~ 我又来乱涂乱画了~~~ 今天来尝试一下有点科幻风格的~~ 目标是画一只 能在天上飞的飞天鲸...