react-native - React Native Reanimated 仅允许在特定区域拖动
问题描述
我正在为有特殊需要的学生开发一个应用程序,以便他们学习如何写作。为此,他们需要大量重复追踪字母,我认为使用 reanimated 将是实现这一目标的最佳方式。到目前为止,我有一个可以在屏幕上任意拖动的圆圈,但我需要的是它只能在特定区域内拖动。该区域将是一个字母(例如“A”),学生需要将其从头拖到尾,并且不能超出它。从本质上讲,他们将追踪这封信。但是,这是我的第一个反应原生项目,我很困惑。谁能帮我解决这个问题?这是我目前拥有的代码:
import React, { useState } from 'react'
import {
StyleSheet,
View,
Dimensions,
} from 'react-native'
import { PanGestureHandler, State } from 'react-native-gesture-handler'
import Animated from "react-native-reanimated"
const { Value, event, cond, eq, add, set, interpolate, extrapolate } = Animated
const {width, height } = Dimensions.get('window')
export default class App extends React.Component {
dragX = new Value(0)
dragY = new Value(0)
gestureState = new Value(-1)
offsetX = new Value(width/2)
offsetY = new Value(height/2)
onGestureEvent = event([
{
nativeEvent: {
translationX: this.dragX,
translationY: this.dragY,
state: this.gestureState
}
}
])
transX = cond(
eq(this.gestureState, State.ACTIVE),
add(this.offsetX, this.dragX),
set(this.offsetX, add(this.offsetX, this.dragX))
)
transY = cond(
eq(this.gestureState, State.ACTIVE),
add(this.offsetY, this.dragY),
set(this.offsetY, add(this.offsetY, this.dragY))
)
opacity = interpolate(this.transY, {
inputRange: [0, height],
outputRange: [0.1,1 ]
})
render() {
return (
<View style={styles.container}>
<PanGestureHandler onGestureEvent={this.onGestureEvent}
onHandlerStateChange={this.onGestureEvent}
>
<Animated.View style={[styles.box, {
opacity: this.opacity,
transform: [{
translateX: this.transX,
translateY: this.transY,
}]
}]}/>
</PanGestureHandler>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
box: {
width: 50,
height: 50,
marginLeft: 34,
marginTop: 115,
backgroundColor: 'blue',
zIndex: 200,
borderRadius: 30,
},
image: {
flex: 1,
resizeMode: 'cover',
justifyContent: 'center',
height: 500,
},
})
任何帮助将不胜感激!
解决方案
推荐阅读
- javascript - 在将视频元素添加到 DOM 之前,如何在用户启动的事件上播放带声音的视频?iOS Safari/Chrome/Webkit
- php - 根据php中的页码更改href
- reactjs - Simplest way to put everything under http://localhost:3000/app instead of http://localhost:3000/ in React
- python - 使用 df.dropna() 返回 NoneType 对象
- r - 当间隔增加时优化到达错误的答案
- authentication - 设计 - 使用非唯一字段对用户进行身份验证
- pandas - 如何将 Google 表格的行导入 Pandas,但使用列名?
- swift - Swift compiler issue when running Xcode 11 using SwiftUI
- tensorflow - tf.keras.optimizer('fo') 中的 optimizer.weights 是什么?
- c# - 如何制作游戏对象数组