javascript - 如何将 PanResponder 与动画值一起使用?
问题描述
我一直在尝试使用 PanResponder 和 React Animated API 制作一个简单的动画。这是我到目前为止所拥有的:
export default class Main extends Component {
constructor(props) {
super(props)
this.state = {
y: new Animated.Value(0)
}
this._panResponder = PanResponder.create({
onMoveShouldSetResponderCapture: () => true,
onMoveShouldSetPanResponderCapture: () => true,
onPanResponderMove: Animated.event([
null,
{
dy: this.state.y
}
]),
});
}
render() {
let { y } = this.state;
return (
<View {...this._panResponder.panHandlers} style={{ flex: 1 }} >
<LinearGradient start={{ x: 1.0, y: 0 }} end={{ x: 0.0, y: 1.0 }} colors={['rgba(0,187,9,0.575)', 'rgba(255, 217, 0 , 0.719)']} style={{ alignItems: "center", justifyContent: "center", margin: 0, flex: 1, padding: 20, width: "100%" }}>
<ResponsiveImage style={{ transform: [{ rotateZ: y + " deg" }, { perspective: 1000 }], marginBottom: 100 }} source={require('./assets/images/logo.png')} initWidth="225" initHeight="220" />
</LinearGradient>
</View>
);
}
}
使用上面附加的代码,旋转动画不起作用。它仅在我使用以下代码代替当前的 onPanResponderMove 方法时才有效。
onPanResponderMove: (a,b)=>{this.setState({y: b.dy}) }
为什么 Animated.Event 方法不起作用?
解决方案
推荐阅读
- statistics - 根据以上信息进行概率计算
- chart.js - Chart.js 使用变量作为数据输入时返回控制台错误并且不显示图表
- javascript - 使用 JavaScript 通过 ESC/POS 将图像打印命令发送到以太网上的热敏打印机
- optimization - 导入 PAO 问题(基于 Python 的对抗优化包)
- ubuntu - 如何在 Ubuntu 21.04 终端中调用 npx cypress open 或 npx cypress run?
- xamarin.forms - 以 Xamarin 形式缩放图像
- ruby-on-rails - Postgresql/rails - 加入不同的值或只为不同的值创建重复的行
- javascript - 如何使用 Barba.js 从单个页面进行多个页面转换
- javascript - 在 github repo 的 Packages 部分下显示 npm 包的链接
- mysql - MySQL连接三个表并显示空值