使用 reanimated, redash 反应原生动画 onScroll 的问题
Problem with react native animation onScroll using reanimated, redash
您好,我正在尝试在 React Native 中实现动画。
当我从滚动视图向上滚动时,我想要滚动视图中的一个子项,即 buttonContainer 淡出。
所以,当我开始向上滚动时,buttonContainer 的不透明度可能会从 1 变为 0。
但是,什么也没有发生。
我不完全明白输入范围和输出范围是什么。
这里还有
import * as React from 'react';
import { Text, View, StyleSheet, Dimensions, Button } from 'react-native';
import Animated from 'react-native-reanimated';
import { onScroll } from 'react-native-redash';
const { height } = Dimensions.get('window');
const BUTTON_CONTAINER_HEIGHT = height / 1.7;
const { Value, interpolate, Extrapolate } = Animated;
export default class App extends React.Component {
render() {
const scrollY = new Value(0);
const opacity = interpolate(scrollY, {
inputRange: [BUTTON_CONTAINER_HEIGHT, height - 30],
outputRange: [1, 0],
extrapolate: Extrapolate.CLAMP,
});
return (
<View style={{ flex: 1 }}>
<View style={styles.container}>
<Animated.ScrollView
onScroll={onScroll({ scrollY })}
showVerticalScrollIndicator={false}
scrollEventThrottle={1}>
<Animated.View
style={[styles.buttonContainer, { opacity: opacity }]}>
<Text>Hello World! </Text>
<Button title="Click Me!" />
</Animated.View>
<View>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
</View>
</Animated.ScrollView>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
position: 'relative',
},
buttonContainer: {
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
alignItems: 'center',
height: BUTTON_CONTAINER_HEIGHT,
marginBottom: 30,
},
textStyle: {
height: 100,
fontSize: 24,
marginTop: 10,
},
});
您以错误的方式定义了动画值。不要在 render 方法中定义值,因为它们会在每次组件重新渲染时创建。相反,在构造函数中定义它们。
使用以下代码:
import * as React from 'react';
import {Text, View, StyleSheet, Dimensions, Button} from 'react-native';
import {onScroll} from 'react-native-redash';
import Animated from 'react-native-reanimated';
const {height} = Dimensions.get('window');
const BUTTON_CONTAINER_HEIGHT = height / 1.7;
const {Value, interpolate, Extrapolate} = Animated;
export default class App extends React.Component<{}> {
constructor(props: Readonly<{}>) {
super(props);
this.scrollY = new Value(0);
}
render() {
const {scrollY} = this;
const opacity = interpolate(scrollY, {
inputRange: [0, BUTTON_CONTAINER_HEIGHT, height - 30],
outputRange: [1, 0, 0],
extrapolate: Extrapolate.CLAMP,
});
return (
<View style={{flex: 1}}>
<View style={styles.container}>
<Animated.ScrollView
onScroll={onScroll({y: scrollY})}
showsVerticalScrollIndicator={false}
scrollEventThrottle={1}>
<Animated.View style={[styles.buttonContainer, {opacity}]}>
<Text>Hello World! </Text>
<Button onPress={() => {}} title="Click Me!" />
</Animated.View>
<View>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
</View>
</Animated.ScrollView>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
position: 'relative',
},
buttonContainer: {
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
alignItems: 'center',
height: BUTTON_CONTAINER_HEIGHT,
marginBottom: 30,
},
textStyle: {
height: 100,
fontSize: 24,
marginTop: 10,
},
});
您好,我正在尝试在 React Native 中实现动画。 当我从滚动视图向上滚动时,我想要滚动视图中的一个子项,即 buttonContainer 淡出。 所以,当我开始向上滚动时,buttonContainer 的不透明度可能会从 1 变为 0。 但是,什么也没有发生。 我不完全明白输入范围和输出范围是什么。
这里还有
import * as React from 'react';
import { Text, View, StyleSheet, Dimensions, Button } from 'react-native';
import Animated from 'react-native-reanimated';
import { onScroll } from 'react-native-redash';
const { height } = Dimensions.get('window');
const BUTTON_CONTAINER_HEIGHT = height / 1.7;
const { Value, interpolate, Extrapolate } = Animated;
export default class App extends React.Component {
render() {
const scrollY = new Value(0);
const opacity = interpolate(scrollY, {
inputRange: [BUTTON_CONTAINER_HEIGHT, height - 30],
outputRange: [1, 0],
extrapolate: Extrapolate.CLAMP,
});
return (
<View style={{ flex: 1 }}>
<View style={styles.container}>
<Animated.ScrollView
onScroll={onScroll({ scrollY })}
showVerticalScrollIndicator={false}
scrollEventThrottle={1}>
<Animated.View
style={[styles.buttonContainer, { opacity: opacity }]}>
<Text>Hello World! </Text>
<Button title="Click Me!" />
</Animated.View>
<View>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
</View>
</Animated.ScrollView>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
position: 'relative',
},
buttonContainer: {
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
alignItems: 'center',
height: BUTTON_CONTAINER_HEIGHT,
marginBottom: 30,
},
textStyle: {
height: 100,
fontSize: 24,
marginTop: 10,
},
});
您以错误的方式定义了动画值。不要在 render 方法中定义值,因为它们会在每次组件重新渲染时创建。相反,在构造函数中定义它们。
使用以下代码:
import * as React from 'react';
import {Text, View, StyleSheet, Dimensions, Button} from 'react-native';
import {onScroll} from 'react-native-redash';
import Animated from 'react-native-reanimated';
const {height} = Dimensions.get('window');
const BUTTON_CONTAINER_HEIGHT = height / 1.7;
const {Value, interpolate, Extrapolate} = Animated;
export default class App extends React.Component<{}> {
constructor(props: Readonly<{}>) {
super(props);
this.scrollY = new Value(0);
}
render() {
const {scrollY} = this;
const opacity = interpolate(scrollY, {
inputRange: [0, BUTTON_CONTAINER_HEIGHT, height - 30],
outputRange: [1, 0, 0],
extrapolate: Extrapolate.CLAMP,
});
return (
<View style={{flex: 1}}>
<View style={styles.container}>
<Animated.ScrollView
onScroll={onScroll({y: scrollY})}
showsVerticalScrollIndicator={false}
scrollEventThrottle={1}>
<Animated.View style={[styles.buttonContainer, {opacity}]}>
<Text>Hello World! </Text>
<Button onPress={() => {}} title="Click Me!" />
</Animated.View>
<View>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
<Text style={styles.textStyle}>This is content</Text>
</View>
</Animated.ScrollView>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
position: 'relative',
},
buttonContainer: {
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
alignItems: 'center',
height: BUTTON_CONTAINER_HEIGHT,
marginBottom: 30,
},
textStyle: {
height: 100,
fontSize: 24,
marginTop: 10,
},
});