forked from hirejohnsalcedo/react-native-workshop-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
64 lines (58 loc) · 2.12 KB
/
Copy pathApp.js
File metadata and controls
64 lines (58 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React, { Component } from "react";
import { View, Text, TouchableOpacity, Dimensions, PanResponder, Animated } from "react-native";
import Horizontal from "./src/components/horizontal/horizontal";
import Vertical from "./src/components/vertical/vertical";
const { width } = Dimensions.get("window");
const images = [
require("./src/assets/1.jpeg"),
require("./src/assets/2.jpeg"),
require("./src/assets/3.jpeg"),
]
export default class App extends Component {
state = {
activeIndex: 0,
}
scaleAnimatedValue = new Animated.Value(1);
handlePanResponderMove = (_, gestureState) => {
const isBlockingFurtherSwipes = this.activeIndexOnPanResponderGrant !== this.state.activeIndex;
if (isBlockingFurtherSwipes) {
return null;
}
const horizontalSwipeVelocity = gestureState.vx;
const horizontalSwipeVelocityThreshold = 0.7;
const hasSwipedRight = horizontalSwipeVelocity < (horizontalSwipeVelocityThreshold * -1);
if (hasSwipedRight) {
this.handleSwipe();
}
}
panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderMove: this.handlePanResponderMove,
onPanResponderGrant: () => this.activeIndexOnPanResponderGrant = this.state.activeIndex,
})
handleSwipe = () => {
this.setState((prevState) => ({ activeIndex: prevState.activeIndex + 1 === images.length ? 0 : prevState.activeIndex + 1 }),
() => this.horizontalScrollViewRef.scrollTo({ x: width * this.state.activeIndex }))
}
render() {
const scaleTransform = this.scaleAnimatedValue.interpolate({
inputRange: [0, 50],
outputRange: [1, 0],
extrapolate: "clamp",
})
return (
<View>
<Animated.View
{...this.panResponder.panHandlers}
style={{ position: "absolute", height: 200, width, zIndex: 999, transform: [{ scaleX: scaleTransform }, { scaleY: scaleTransform }] }} />
<Horizontal
images={images}
horizontalScrollViewRef={(ref) => this.horizontalScrollViewRef = ref}
/>
<Vertical
scaleAnimatedValue={this.scaleAnimatedValue}
/>
</View>
)
}
}