[ 卡卡 DAY 18 ] - React Native animated 来简单使用 translate 效果

记得第一次使用到 css 的 animation 跟 transform 系列效果
做了很多厉害的动画东西飞来飞去转来转去
这次我们来使用 React Native animated 来做简单的动画吧!

动画

开始实作

经过前两篇的介绍,大致简单的使用都了解了吧~所以我们就直接开始噜!

  1. 於 components/ 建立 AnimatedTranslate.js 引入 animated
import { Animated } from 'react-native';
  1.  加入动画事件
    这次我们做两个按钮
    一个叫做 Move Me
    另一个叫做 Back Me
  • 先将画面做好 return
<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();
  };

整个 AnimatedTranslate.js

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 球球滚出去 (下图)

https://ithelp.ithome.com.tw/upload/images/20210930/20142011O0nOciW9fO.jpg

按 Back Me 球球滚回来(下图)
https://ithelp.ithome.com.tw/upload/images/20210930/20142011dLxXKsRhv6.jpg

Day 18 done!! 请多多指教


<<:  19 首页与开始游戏按钮

>>:  [前端暴龙机,Vue2.x 进化 Vue3 ] Day24.Vue3 Options API & Composition API 介绍

Laravel 技术笔记 (一)【Routing 路由】

序言 此系列文章将不定期更新,献给未来健忘的自己,也献给刚接触 Laravel 的初学者们,若您在观...

Angular#4 专案:路由 建置

Angular [目标] 启动程序先导入Login元件 1. 新增元件、模组 Syntax:ng h...

Day 20: Behavioral patterns - Interpreter

目的 取得一段讯息後,解析、转译成具有特定含义的讯息。 说明 简单来说,就是讯息的转换器。 当有分析...

[Day 14]现在真的履历导向比较好吗(後端篇)

挑战目标: MockNative Camp 最近不止工作还有很多事情要处理,今天只能先暂停一次。 之...

DAY 3 - 飞天鲸鲨

大家好~ 我又来乱涂乱画了~~~ 今天来尝试一下有点科幻风格的~~ 目标是画一只 能在天上飞的飞天鲸...