我们只能通过使用变量来为状态栏设置动画。(通过切换)

让我们将黑暗作为变量。
state = {
dark: true,
};
最初,这是真的。所以状态栏样式应该是深色的。
<StatusBar
barStyle={dark ? 'dark-content' : 'light-content'}
translucent
backgroundColor="transparent"
/>
切换是在滚动方法内部完成的。这里我使用 100 作为偏移量。
onScroll = ({nativeEvent}) => {
let y = nativeEvent.contentOffset.y;
this.scroll.setValue(y); // set scroll animation value here
const {dark} = this.state;
let scrollValue = y;
if (scrollValue > 100 && dark) {
this.setState({dark: false});
}
if (scrollValue < 100 && !dark) {
this.setState({dark: true});
}
};
示例代码
import React, {Component} from 'react';
import {
View,
StatusBar,
StyleSheet,
ScrollView,
Dimensions,
Animated,
} from 'react-native';
const {height} = Dimensions.get('window');
export default class Home extends Component {
scroll = new Animated.Value(0);
state = {
dark: true,
};
onScroll = ({nativeEvent}) => {
let y = nativeEvent.contentOffset.y;
this.scroll.setValue(y); // set scroll animation value here
const {dark} = this.state;
let scrollValue = y;
if (scrollValue > 100 && dark) {
this.setState({dark: false});
}
if (scrollValue < 100 && !dark) {
this.setState({dark: true});
}
};
render() {
const {dark} = this.state;
return (
<View style={styles.container}>
<StatusBar
barStyle={dark ? 'dark-content' : 'light-content'}
translucent
backgroundColor="transparent"
/>
<ScrollView
style={styles.ScrollView}
contentContainerStyle={styles.scrollContainer}
onScroll={this.onScroll}>
<View style={styles.item} />
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
ScrollView: {
flex: 1,
},
scrollContainer: {
height: height * 2,
backgroundColor: '#000',
},
item: {
height: 100,
backgroundColor: '#fff',
},
});