我的代码
index
/**
* @format
*/
import { AppRegistry, Platform, UIManager } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
if (Platform.OS === 'android') {
if (UIManager.setLayoutAnimationEnabledExperimental) {
UIManager.setLayoutAnimationEnabledExperimental(true);
console.log("LayoutAnimation enabled for Android");
} else {
console.log("LayoutAnimation enabling failed or not supported");
}
}
AppRegistry.registerComponent(appName, () => App);
Anim10
import React, { useState } from 'react';
import {
StyleSheet,
View,
Button,
LayoutAnimation,
Image,
Text,
UIManager,
} from 'react-native';
import icon_avatar from '../assets/images/default_avatar.png';
export default () => {
const [showView, setShowView] = useState(false);
const [showRight, setShowRight] = useState(false);
return (
<View style={styles.root}>
<Button
title="按钮"
onPress={() => {
// LayoutAnimation.configureNext(LayoutAnimation.Presets.linear);
// 配置动画
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring, () => {
console.log(`动画执行完毕`);//打印了
}, () => {
console.log(`动画执行失败`);
});
// setShowView(!showView);
setShowView(true);
// 切换状态
// setShowRight(!showRight);
}}
/>
{showView && <View
style={[
styles.view,
{
flexDirection: showRight ? 'row-reverse' : 'row',
// marginHorizontal: showRight ? 20 : 0, // 改变 margin 触发动画
},
]}
>
<Image style={styles.img} source={icon_avatar} />
<Text style={styles.txt}>这是一行自我介绍的文本</Text>
</View>}
</View>
);
};
const styles = StyleSheet.create({
root: {
width: '100%',
height: '100%',
backgroundColor: 'white',
justifyContent: 'center',
alignItems: 'center',
},
view: {
width: '100%',
height: 100,
backgroundColor: '#F0F0F0',
marginTop: 20,
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 16,
},
img: {
width: 64,
height: 64,
borderRadius: 32,
},
txt: {
fontSize: 20,
color: '#303030',
fontWeight: 'bold',
marginHorizontal: 20,
},
});
然后不管怎么设置,手机上都没有效果。