
- React Native 教程
- React Native - 主頁
- 核心概念
- React Native - 概覽
- React Native - 環境設定
- React Native - 應用
- React Native - 狀態
- React Native - 屬性
- React Native - 樣式
- React Native - 伸縮佈局
- React Native - 列表檢視
- React Native - 文字輸入
- React Native - 滾動檢視
- React Native - 圖片
- React Native - HTTP
- React Native - 按鈕
- React Native - 動畫
- React Native - 除錯
- React Native - 路由
- React Native - 執行 IOS
- React Native - 執行 Android
- 元件與 API
- React Native - 檢視
- React Native - 網頁檢視
- React Native - 模態
- React Native - 活動指示器
- React Native - 選擇器
- React Native - 狀態列
- React Native - 開關
- React Native - 文字
- React Native - 警告
- React Native - 地理定位
- React Native - 非同步儲存
- React Native 有用資源
- React Native - 快速指南
- React Native - 有用資源
- React Native - 討論
React Native - 動畫
在本章中,我們將展示如何在 React Native 中使用 LayoutAnimation。
動畫元件
我們將把 myStyle 作為狀態的一個屬性設定。此屬性用於對 PresentationalAnimationComponent 中的元素進行樣式設定。
我們還將建立兩個函式 - expandElement 和 collapseElement。這些函式將更新狀態中的值。第一個將使用 spring 預設動畫,而第二個將具有 linear 預設。我們也將這些作為屬性傳遞。Expand 和 Collapse 按鈕呼叫了 expandElement() 和 collapseElement() 函式。
在此示例中,我們將動態更改框的寬度和高度。因為 Home 元件將保持不變,所以我們只會更改 Animations 元件。
App.js
import React, { Component } from 'react' import { View, StyleSheet, Animated, TouchableOpacity } from 'react-native' class Animations extends Component { componentWillMount = () => { this.animatedWidth = new Animated.Value(50) this.animatedHeight = new Animated.Value(100) } animatedBox = () => { Animated.timing(this.animatedWidth, { toValue: 200, duration: 1000 }).start() Animated.timing(this.animatedHeight, { toValue: 500, duration: 500 }).start() } render() { const animatedStyle = { width: this.animatedWidth, height: this.animatedHeight } return ( <TouchableOpacity style = {styles.container} onPress = {this.animatedBox}> <Animated.View style = {[styles.box, animatedStyle]}/> </TouchableOpacity> ) } } export default Animations const styles = StyleSheet.create({ container: { justifyContent: 'center', alignItems: 'center' }, box: { backgroundColor: 'blue', width: 50, height: 100 } })
廣告