首页 > 其他分享 >react-native手势动画制作精美卡片拖拽效果

react-native手势动画制作精美卡片拖拽效果

时间:2023-02-14 15:33:47浏览次数:42  
标签:const return react state action false Animated 拖拽 native


react-native手势动画制作精美卡片拖拽效果_react native


视频学习地址https://www.bilibili.com/video/BV1h7411o7zE

代码

import React, { Component } from 'react'
import {
StyleSheet,
Text,
View,
PanResponder,
Animated,
Image,
Dimensions
} from 'react-native'
import styled from 'styled-components'
import Card from './Card'
import { connect } from 'react-redux'
import Login from './login/Login'

const screenWidth = Dimensions.get('window').width
const screenHeight = Dimensions.get('window').height + 50

class Home extends Component {
state = {
backgroundColor: '#fffae5',
pan: new Animated.ValueXY(0, 0),
scale: new Animated.Value(0.9),
translateY: new Animated.Value(40),
thirdTranslateY: new Animated.Value(20),
dataIndex: 0
}
UNSAFE_componentWillMount() {
this._panResponder = PanResponder.create({
onMoveShouldSetPanResponder: (event,gestureState) => {
if(gestureState.dx === 0 && gestureState.dy === 0) {
return false
} else {
if(this.props.action === 'openCard') {
return false
} else {
return true
}
}
},
onPanResponderGrant: () => {
Animated.spring(this.state.scale, { toValue: 1, useNativeDriver: false }).start()
Animated.spring(this.state.translateY, { toValue: 0, useNativeDriver: false }).start()
this.setState({ backgroundColor: '#ccc' })
Animated.spring(this.state.thirdTranslateY, { toValue: 45, useNativeDriver: false }).start()
},
onPanResponderMove: Animated.event([
null,
{ dx: this.state.pan.x, dy: this.state.pan.y }
],
{ useNativeDriver: false }),
onPanResponderRelease: () => {
const currentY = this.state.pan.y.__getValue();
if (currentY > 250) {
Animated.timing(this.state.pan, {
toValue: { x: 0, y: 800 },
duration: 200,
useNativeDriver: false
}).start(() => {
Animated.timing(this.state.pan, {
toValue: { x: 0, y: 0 },
duration: 0,
useNativeDriver: false
}).start()
this.setState({ dataIndex: shouldIndex(this.state.dataIndex) })
Animated.spring(this.state.scale, { toValue: 0.9, useNativeDriver: false }).start()
})
} else {
Animated.spring(this.state.pan, {
toValue: { x: 0, y: 0 },
useNativeDriver: false
}).start()
Animated.spring(this.state.scale, { toValue: 0.9, useNativeDriver: false }).start()
Animated.spring(this.state.translateY, { toValue: 40, useNativeDriver: false }).start()
Animated.spring(this.state.thirdTranslateY, { toValue: 20, useNativeDriver: false }).start()
}
this.setState({ backgroundColor: '#fffae5' })
}
})
}

render() {
const { scale, translateY, backgroundColor, thirdScale, thirdTranslateY, dataIndex } = this.state
return (
<View style={[styles.container, { backgroundColor: backgroundColor }]}>
<Animated.View style={
{
transform: [
{ translateX: this.state.pan.x, },
{ translateY: this.state.pan.y, }
]
}}
{...this._panResponder.panHandlers}
>
<Card canOpen={true} {...CardData[dataIndex]} />
</Animated.View>
<Animated.View style={[
styles.otherCard,
{
transform: [
{ scale: scale },
{ translateY: translateY }
]
}]}
>
<Card {...CardData[shouldIndex(dataIndex)]} />
</Animated.View>
<Animated.View style={[
styles.otherCard,
{
transform: [
{ scale: 0.9 },
{ translateY: thirdTranslateY }
],
zIndex: -2
}]}
>
<Card {...CardData[shouldIndex(dataIndex + 1)]} />
</Animated.View>
{/* <Login /> */}
</View>
);
}
}

const styles = StyleSheet.create({
otherCard: {
position: 'absolute',
zIndex: -1
},
container: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ddd',
width: '100%',
height: screenHeight
}
});

function shouldIndex(index) {
let shouldIndex = index + 1
if (shouldIndex > CardData.length - 1) {
return 0
}
return shouldIndex
}

const CardData = [
{
image: require('../assets/0.jpg'),
title: 'Awesome Styled Components',
startNum: 29.5,
info: 'Visual primitives for the component age Use the best bits of ES6 and CSS to style your apps without stress

标签:const,return,react,state,action,false,Animated,拖拽,native
From: https://blog.51cto.com/u_15964288/6056979

相关文章

  • react-native仿写洛克王国手游版
    首先声明此项目完全是出于学习和对洛克王国的喜爱而生,项目中使用的游戏宠物若涉及侵权请联系我前两天和朋友聊天说到洛克王国没有手游,正好最近在学习react-native,觉得可以尝......
  • react豆瓣API获取电影数据小demo
    entd使用使用entd的layout布局快速搭建页面​​entd官网​​页面刷新,路由不刷新,设置sider和路由同步,在componentWillMount还是componentDidMount都获取不到路由信息,但是能......
  • 只允许在指定范围里拖拽
    废话不多说代码如下我的查看代码letoDiv=document.querySelector('div')letflag=false;oDiv.addEventListener('mousedown',(e)=>{letx=e......
  • Jest + React 单元测试最佳实践
    我们是袋鼠云数栈UED团队,致力于打造优秀的一站式数据中台产品。我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值。前言单元测试是一种用于测试“单元”的......
  • react typescript封装函数式组件styled-component
    interfaceIButton{margin?:boolean;width?:string;justify?:JustifyContentProps;}constButton=styled.button<IButton>`.....`;interfaceILoadButtonext......
  • 前端二面经典react面试题
    如何解决props层级过深的问题使用ContextAPI:提供一种组件之间的状态共享,而不必通过显式组件树逐层传递props;使用Redux等状态库。react实现一个全局的dialogimpo......
  • 问:你是如何进行react状态管理方案选择的?
    前言:最近接触到一种新的(对我个人而言)状态管理方式,它没有采用现有的开源库,如redux、mobx等,也没有使用传统的useContext,而是用useState+useEffect写了一个发布订阅者模式进......
  • 京东前端react面试题及答案
    useEffect与useLayoutEffect的区别(1)共同点运用效果:useEffect与useLayoutEffect两者都是用于处理副作用,这些副作用包括改变DOM、设置订阅、操作定时器等。在函数......
  • 问:React的useState和setState到底是同步还是异步呢?
    先来思考一个老生常谈的问题,setState是同步还是异步?再深入思考一下,useState是同步还是异步呢?我们来写几个demo试验一下。先看useState同步和异步情况下,连续执行两......
  • React.cloneElement
    React.cloneElement()接收三个参数第一个参数接收一个ReactElement,可以是真实的dom结构也可以是自定义的。第二个参数返回旧元素的props、key、ref。可以添加新的props,第......