将原生动画多个视图作为一个圆圈进行反应(react native animating multiple views as a circle)
我想要2个视图,它们像一个圆圈一样变换,同时没有旋转。 第一个视图从顶部开始,第二个视图从底部开始。 我已经问过如何使用一个视图来完成它。 我不认为它运行两个视图。 之前的问题
//import liraries import React, { Component } from 'react'; import { View, Text, StyleSheet, Animated, Button, TouchableOpacity } from 'react-native'; // create a component export default class App extends Component { constructor() { super() this.animated = new Animated.Value(0); this.animated2 = new Animated.Value(0); var range = 1, snapshot = 50, radius = 100; /// translateX var inputRange = [] var outputRange = [] var outputRange2 = [] for (var i=0; i<=snapshot; ++i) { var value = i/snapshot; var move = Math.sin(value * Math.PI * 2) * radius; inputRange.push(value); outputRange.push(move); outputRange2.push(-move); } translateX = this.animated.interpolate({ inputRange, outputRange }); translateX2 = this.animated2.interpolate({inputRange, outputRange2}) /// translateY var inputRange = [] var outputRange = [] var outputRange2 = [] for (var i=0; i<=snapshot; ++i) { var value = i/snapshot; var move = -Math.cos(value * Math.PI * 2) * radius; inputRange.push(value); outputRange.push(move); outputRange2.push(-move); } translateY = this.animated.interpolate({ inputRange, outputRange }); translateY2 = this.animated2.interpolate({inputRange, outputRange2}) } animate() { this.animated.setValue(0) Animated.timing(this.animated, { toValue: 1, duration: 10000, }).start(); this.animated2.setValue(0) Animated.timing(this.animated2, { toValue: 1, duration: 10000, }).start(); } render() { //const transform = [{ translateY: this.translateY }, {translateX: this.translateX}]; return ( <View style={styles.container}> <Animated.View style={ [{ transform: [{ translateY: translateY }, {translateX: translateX}] }]}> <TouchableOpacity style={styles.btn}> <Text>hallo</Text> </TouchableOpacity> </Animated.View> <Animated.View style={ [{ transform: [{ translateY: translateY2 }, {translateX: translateX2}] }]}> <TouchableOpacity style={styles.btn}> <Text>hallo</Text> </TouchableOpacity> </Animated.View> <Button title="Test" onPress={() => { this.animate() }} /> </View> ); } } // define your styles const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#2c3e50', }, btn2: { justifyContent: 'center', alignItems: 'flex-end', alignSelf: 'flex-end' }, btn: { backgroundColor: 'red', justifyContent: 'center', alignItems: 'center', width: 50, } });
I want 2 views which transform like a circle with no rotation at the same time. The first view starts at the top and the second view at the bottom. I already asked how to do it with one view. I dont get it run with two views. Question Before
//import liraries import React, { Component } from 'react'; import { View, Text, StyleSheet, Animated, Button, TouchableOpacity } from 'react-native'; // create a component export default class App extends Component { constructor() { super() this.animated = new Animated.Value(0); this.animated2 = new Animated.Value(0); var range = 1, snapshot = 50, radius = 100; /// translateX var inputRange = [] var outputRange = [] var outputRange2 = [] for (var i=0; i<=snapshot; ++i) { var value = i/snapshot; var move = Math.sin(value * Math.PI * 2) * radius; inputRange.push(value); outputRange.push(move); outputRange2.push(-move); } translateX = this.animated.interpolate({ inputRange, outputRange }); translateX2 = this.animated2.interpolate({inputRange, outputRange2}) /// translateY var inputRange = [] var outputRange = [] var outputRange2 = [] for (var i=0; i<=snapshot; ++i) { var value = i/snapshot; var move = -Math.cos(value * Math.PI * 2) * radius; inputRange.push(value); outputRange.push(move); outputRange2.push(-move); } translateY = this.animated.interpolate({ inputRange, outputRange }); translateY2 = this.animated2.interpolate({inputRange, outputRange2}) } animate() { this.animated.setValue(0) Animated.timing(this.animated, { toValue: 1, duration: 10000, }).start(); this.animated2.setValue(0) Animated.timing(this.animated2, { toValue: 1, duration: 10000, }).start(); } render() { //const transform = [{ translateY: this.translateY }, {translateX: this.translateX}]; return ( <View style={styles.container}> <Animated.View style={ [{ transform: [{ translateY: translateY }, {translateX: translateX}] }]}> <TouchableOpacity style={styles.btn}> <Text>hallo</Text> </TouchableOpacity> </Animated.View> <Animated.View style={ [{ transform: [{ translateY: translateY2 }, {translateX: translateX2}] }]}> <TouchableOpacity style={styles.btn}> <Text>hallo</Text> </TouchableOpacity> </Animated.View> <Button title="Test" onPress={() => { this.animate() }} /> </View> ); } } // define your styles const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#2c3e50', }, btn2: { justifyContent: 'center', alignItems: 'flex-end', alignSelf: 'flex-end' }, btn: { backgroundColor: 'red', justifyContent: 'center', alignItems: 'center', width: 50, } });
原文:https://stackoverflow.com/questions/47923061
最满意答案
@ pe8ter让我走上了正确的道路。 事实证明我的服务器端代码没有问题,这是我的客户端代码。 我使用fetch来使用api,但我没有处理fetch正确返回的承诺。 我需要做的就是改变我的客户端代码:
export function fetchVerificationLink() { return async( dispatch ) => { dispatch(requestVerificationLink()); let response, link; try { response = await read(urls.etradeVerification); // I have a wrapper around node-fetch, but failed to handle the promise that fetch returns dispatch(receiveVerificationLink(response)); } catch ( error ) { console.error('Unable to get verification link from eTrade.', error); } } }
我将代码更改为此以处理来自fetch的响应:
export function fetchVerificationLink() { return async( dispatch ) => { dispatch(requestVerificationLink()); let response, link; try { response = await read(urls.etradeVerification); try { link = await response.json(); // Here was the solution to my problem. dispatch(receiveVerificationLink(link)); } catch ( error ) { console.error('Unable to parse authorization link response.', error); } } catch ( error ) { console.error('Unable to get verification link from eTrade.', error); } } }
现在一切都按预期工作了。 谢谢你的帮助!
@pe8ter got me onto the right path. Turns out there wasn't a problem with my server-side code, it was my client-side code. I was using fetch to consume the api, but I wasn't handling the promise that fetch returns correctly. All I needed to do was change my client side code from this:
export function fetchVerificationLink() { return async( dispatch ) => { dispatch(requestVerificationLink()); let response, link; try { response = await read(urls.etradeVerification); // I have a wrapper around node-fetch, but failed to handle the promise that fetch returns dispatch(receiveVerificationLink(response)); } catch ( error ) { console.error('Unable to get verification link from eTrade.', error); } } }
I changed the code to this to handle the response from fetch:
export function fetchVerificationLink() { return async( dispatch ) => { dispatch(requestVerificationLink()); let response, link; try { response = await read(urls.etradeVerification); try { link = await response.json(); // Here was the solution to my problem. dispatch(receiveVerificationLink(link)); } catch ( error ) { console.error('Unable to parse authorization link response.', error); } } catch ( error ) { console.error('Unable to get verification link from eTrade.', error); } } }
And now everything is working as expected. Thank you for the assist!
相关问答
更多-
我也有同样的问题。 只有当所有中间件都完成时,koa才会结束响应(在application.js中 , 响应是响应中间件,它会结束响应。) app.callback = function(){ var mw = [respond].concat(this.middleware); var gen = compose(mw); var fn = co.wrap(gen); var self = this; if (!this.listeners('error').length) this ...
-
ctx.body = await et.getRequestToken(receiveVerificationUrl,failedToGetUrl); 对et.getRequestToken的这个调用不会返回任何内容。 当await发射时,它将被定义为未定义。 通常我建议使用es6-promisify,但它也不是标准的Node接口(一个回调,带有错误和值参数(非常令人失望!)) 也许创建一个像Promisify函数如下的函数: function getRequestToken(et){ return ...
-
根据自述文件 : Koa 2的解决方法 npm install co --save 然后... import co from 'co'; import render from 'koa-ejs'; render(app, options); app.context.render = co.wrap(app.context.render); app.use(async (ctx, next) => { await ctx.render(view, locals); }); Per the RE ...
-
您似乎安装了较新的版本,可在https://github.com/alexmingoia/koa-router/tree/master上找到 您需要使用new Router() 。 也看起来你正在将koa-router与koa-route ,这是两个不同的包。 const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const router = new Router(); route ...
-
Koa路由器不嵌套(Koa-router not nesting)[2022-11-12]
当你使用嵌套路由时,你需要使用router.use而不是router.get 。 When you are using nested routes you need to use router.use instead of router.get. -
您正在使用multipart/form-data作为表单数据编码,因此在您的服务器代码中,您应该将正确的选项传递给koa-body构造函数。 kaoBody = require('koa-body)({ multipart: true }); You are using multipart/form-data as your form data encoding, so in your server code you should pass the correct option to koa-body c ...
-
@ pe8ter让我走上了正确的道路。 事实证明我的服务器端代码没有问题,这是我的客户端代码。 我使用fetch来使用api,但我没有处理fetch正确返回的承诺。 我需要做的就是改变我的客户端代码: export function fetchVerificationLink() { return async( dispatch ) => { dispatch(requestVerificationLink()); let response, link; ...
-
我必须使用路由器上的中间件才能工作。 // more code above... var handlebars = require("koa-handlebars"); var router = require('koa-router'); var myRouter = new router(); myRouter.use(handlebars({ defaultLayout: 'main' })); myRouter.get('/', function* () { yield th ...
-
如果它是重要的,我认为值得一提的是我使用获取API来发出POST和GET请求。 fetch API提到“默认情况下,fetch不会向服务器发送任何cookie,如果站点依赖于维护用户会话,则会导致未经身份验证的请求。” 如果你想fetch发送cookie,你需要添加一个选项到你发出的被称为credentials的请求中,并将其设置为包含值。 示例POST请求: const request = { method: 'POST', credentials: 'include', headers: . ...
-
Koa w /客户端路由器(Koa w/client side router)[2023-11-15]
刷新页面时,服务器必须回复某些内容; 在这种情况下,它需要使用index.html进行响应,以便客户端应用程序可以启动,然后React Router可以根据URL安装正确的组件。 因此,在服务器端,您需要告诉Koa为每个尚未与任何其他路由匹配的URL提供index.html 。 Solution (based on the answer above) import router from 'koa-router'; import sendfile from 'koa-sendfile'; //code to ...