- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试在将作品释放到其原始方 block 时构建带有动画的简单拖放
目标很简单,就是拖动硬币,放开硬币后,它们会回到自己的单元格中。但是棋子返回的动画有点奇怪。例如,如果将红色硬币拖到右下角的单元格中,则动画从左下角的单元格开始,不会进入直线!
这是页面的代码,可以直接集成到你的RN应用中,如果你有和下面一样的package.json:
import React, { Component } from 'react';
import { StyleSheet, View, Animated, PanResponder, Easing } from 'react-native';
import _ from 'underscore';
class Square {
constructor(value, origin, cellsSize) {
this.value = value;
this.pan = new Animated.ValueXY();
this.cellsSize = cellsSize;
this.boardSize = 3 * this.cellsSize;
this.minXY = this.cellsSize * (0.5);
this.maxXY = this.cellsSize * (1.5);
this.midXY = this.cellsSize;
this.origin = origin;
this.constrainedX = this.pan.x.interpolate({
inputRange: [this.minXY, this.midXY, this.maxXY],
outputRange: [this.minXY, this.midXY, this.maxXY],
extrapolate: 'clamp',
});
this.constrainedY = this.pan.y.interpolate({
inputRange: [this.minXY, this.midXY, this.maxXY],
outputRange: [this.minXY, this.midXY, this.maxXY],
extrapolate: 'clamp',
});
const x = parseInt(this.cellsSize * (0.5 + this.origin.file));
const y = parseInt(this.cellsSize * (0.5 + this.origin.rank));
this.pan.setValue({ x, y });
this.panResponder = this._buildPanResponder();
}
get valueString() {
return this.value;
}
get panRef() {
return this.pan;
}
get panResponderRef() {
return this.panResponder;
}
_buildPanResponder() {
return PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderGrant: (event, gestureState) => {
this.pan.setOffset({ x: this.pan.x._value, y: this.pan.y._value });
},
onPanResponderMove: (event, gestureState) => {
this.pan.setValue({ x: gestureState.dx, y: gestureState.dy });
},
onPanResponderRelease: (event, gesture) => {
const nativeEvent = event.nativeEvent;
const origX = parseInt(this.cellsSize * (this.origin.file + 0.5));
const origY = parseInt(this.cellsSize * (this.origin.rank + 0.5));
Animated.timing(
this.pan,
{
toValue: { x: origX, y: origY },
duration: 400,
delay: 0,
easing: Easing.linear
}
).start();
this.pan.flattenOffset()
}
});
}
}
export default class TestComponent extends Component {
constructor(props) {
super(props);
this.cellsSize = 100;
this.squares = [
new Square('red', { file: 1, rank: 0 }, this.cellsSize),
new Square('green', { file: 0, rank: 1 }, this.cellsSize),
new Square('blue', { file: 1, rank: 1 }, this.cellsSize),
];
}
renderACoin(value, file, rank) {
if (value) {
let style;
switch (value.valueString) {
case 'red': style = styles.redCoin; break;
case 'green': style = styles.greenCoin; break;
case 'blue': style = styles.blueCoin; break;
}
const randomKey = parseInt(Math.random() * 1000000).toString()
return (
<Animated.View key={randomKey} style={StyleSheet.flatten([style,
{
left: value.constrainedX,
top: value.constrainedY,
}])}
{...value.panResponderRef.panHandlers }
/>
);
}
}
renderAllCoins() {
return _.map(this.squares, (currSquare) => {
return this.renderACoin(currSquare, currSquare.origin.file, currSquare.origin.rank);
});
}
render() {
return (
<View style={styles.topLevel}>
<View style={StyleSheet.flatten([styles.board])}
ref="boardRoot"
>
<View style={StyleSheet.flatten([styles.whiteCell, {
left: 50,
top: 50,
}])} />
<View style={StyleSheet.flatten([styles.blackCell, {
left: 150,
top: 50,
}])} />
<View style={StyleSheet.flatten([styles.blackCell, {
left: 50,
top: 150,
}])} />
<View style={StyleSheet.flatten([styles.whiteCell, {
left: 150,
top: 150,
}])} />
{this.renderAllCoins()}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
topLevel: {
backgroundColor: "#CCFFCC",
flex: 1,
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
},
board: {
width: 300,
height: 300,
backgroundColor: "#FFCCFF",
},
whiteCell: {
width: 100,
height: 100,
backgroundColor: '#FFAA22',
position: 'absolute',
},
blackCell: {
width: 100,
height: 100,
backgroundColor: '#221122',
position: 'absolute',
},
greenCoin: {
width: 100,
height: 100,
position: 'absolute',
backgroundColor: '#23CC12',
borderRadius: 50,
},
redCoin: {
width: 100,
height: 100,
position: 'absolute',
backgroundColor: '#FF0000',
borderRadius: 50,
},
blueCoin: {
width: 100,
height: 100,
position: 'absolute',
backgroundColor: '#0000FF',
borderRadius: 50,
},
});
这是我正在使用的 package.json
{
"name": "test",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.0.0-beta.5",
"react-native": "0.49.3",
"underscore": "^1.8.3"
},
"devDependencies": {
"babel-jest": "21.2.0",
"babel-preset-react-native": "4.0.0",
"jest": "21.2.1",
"react-devtools-core": "^2.5.2",
"react-test-renderer": "16.0.0-beta.5"
},
"jest": {
"preset": "react-native"
}
}
每个 Square 的实现都归功于 Square 类,它包含原始方 block 、拖放平移响应器和平移动画值。由于两个 x/y 插值器,拖放动画被限制在单元格内。
我的猜测是奇怪的动画行为是由插值器引起的,或者是我忘记设置为平移动画 XY 值的某个值,但我不能确定。
最佳答案
这是因为您的偏移量的解析方式。您的 toValue
坐标在未应用任何偏移时是正确的,因此您应该在 偏移被展平后开始动画。否则,您将开始(在调用 flattenOffset
之前)从释放点到错误的终点,并且当偏移被展平时“更正”了终点坐标,但起点现在是错误的。如果您放慢动画速度并将 flattenOffset
调用放在 setTimeout
中,以便它在动画中间发生,您可以更清楚地看到这一点。
要修复,只需将 flattenOffset()
调用移到 start()
之前。
onPanResponderRelease: (event, gestureState) => {
const nativeEvent = event.nativeEvent;
const origX = parseInt(this.cellsSize * (this.origin.file + 0.5));
const origY = parseInt(this.cellsSize * (this.origin.rank + 0.5));
// Our animated path should be calculated without an offset, as our
// origX and origY are both un-offset, so flattenOffset() before start()
this.pan.flattenOffset();
Animated.timing(
this.pan,
{
toValue: { x: origX, y: origY },
duration: 400,
delay: 0,
easing: Easing.linear
}
).start();
}
一旦上述问题得到解决,这一点就会变得更加明显,您可以看到发生了什么。这只是因为您的 pan
正在从 释放点 动画回到圆的原点,但圆本身受到约束。因此,如果您的释放点在受限区域之外,您会看到圆圈沿着受限区域的边缘水平或垂直移动,尽可能靠近 pan
,直到 pan
值在框内移动,圆圈可以跟随它移动。
如何处理取决于您想要的行为。假设您不关心平移在受限区域外多远被释放,而您只希望圆圈从它出现的位置线性动画回到它的原点,那么最简单的事情就是设置您的 pan
在开始动画之前将值赋给自身的受限版本:
onPanResponderRelease: (event, gestureState) => {
const nativeEvent = event.nativeEvent;
const origX = parseInt(this.cellsSize * (this.origin.file + 0.5));
const origY = parseInt(this.cellsSize * (this.origin.rank + 0.5));
// Our animated path should be calculated without an offset, as our
// origX and origY are both un-offset, so flattenOffset() before start()
this.pan.flattenOffset();
// Act as if we have released from the centre of where the circle appears
// on screen, rather than potentially outside the constrained area
this.pan.setValue({ x: this.constrainedX.__getValue(), y: this.constrainedY.__getValue() });
Animated.timing(
this.pan,
{
toValue: { x: origX, y: origY },
duration: 400,
delay: 0,
easing: Easing.linear
}
).start();
}
如您所见,这使用“私有(private)”__getValue()
方法作为使用已约束值的便捷方式。如果你想避免这种情况,你必须在 gestureState
中使用坐标并应用你自己的约束逻辑——不幸的是,RN 没有公开一种在非动画值上使用其插值逻辑的方法。
关于android - React-Native 为什么动画不是线性的并且来自发布的地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46974836/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
操作无法完成。 Places API 库中发生内部错误。如果您认为此错误代表错误,请使用我们社区和支持页面 (https://developers.google.com/places/support)
我正在尝试在我的项目中使用 google places,我将其设置在 fragment 中而不是 Activity 中,我的自动完成 fragment 在 fragment 中。但是,当我尝试搜索它时
我的目的是使用R来查询google api。 我有一个地址和名称列表(属于商店、餐馆等),我需要为每个地址和名称存储: “纬度”、“经度”、“业务类型” 我的想法是使用 google place ap
我正在寻找设置一个自动完成的谷歌地方小部件。 我有一个带有“searchFieldText”id 的输入类型文本。 这是我的 JS 代码: var inputsec = document.getEle
是否可以使用图形 API(或地址/ zip )按纬度/经度和半径获取地点?我在文档中的任何地方都看不到它 最佳答案 搜索 URL 的以下格式将返回某个位置附近的地点列表: https://graph.
我正在探索 Google API,主要是 Places API。由于对 Google Places API 的请求数限制为 100,000,因此我正在寻找方法来最大限度地减少发送到 API 的请求数。
伙计们,我在我的应用程序中有一个功能,可以使用 GetFiles 在特定目录中搜索特定文件。方法 System.IO.Directory.GetFiles(string path, string
我已经在 Laravel 5.3 上使用 where 查询成功创建了许多函数,但是这次发生了一些奇怪的事情。 public function show($id){ $artikel = Art
我正在为我的 iPhone 应用程序使用 Facebook 图形 API 来获取附近地点的列表,我使用带有一些参数的“搜索”请求。我得到的响应是一个包含以下信息的地点列表:“纬度”、“经度”、“名称”
我有一个 Android 应用程序,我在其中使用 Google map 显示附近的地方,如加油站、药店等。我正在使用 map 和地点 API。 https://maps.googleapis.com/
我是一名优秀的程序员,十分优秀!