forked from ollija/react-native-sortable-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customAnimationExample.js
110 lines (97 loc) · 2.69 KB
/
customAnimationExample.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React, { Component } from 'react'
import {
StyleSheet,
Text,
Animated,
View
} from 'react-native'
import SortableGrid from 'react-native-sortable-grid'
export default class basicExample extends Component {
constructor() {
super()
this.numbers = [0,1,2,3,4,5,6,7,8,9,10,11]
this.state = {
animation: new Animated.Value(0),
}
}
getColor() {
let r = this.randomRGB()
let g = this.randomRGB()
let b = this.randomRGB()
return 'rgb(' + r + ', ' + g + ', ' + b + ')'
}
randomRGB = () => 160 + Math.random()*85
startCustomAnimation = () => {
console.log("Custom animation started!")
Animated.timing(
this.state.animation,
{ toValue: 100, duration: 500 }
).start( () => {
Animated.timing(
this.state.animation,
{ toValue: 0, duration: 500 }
).start()
})
}
getDragStartAnimation = () => {
return { transform: [
{
scaleX: this.state.animation.interpolate({
inputRange: [0, 100],
outputRange: [1, -1.5],
})
},
{
scaleY: this.state.animation.interpolate({
inputRange: [0, 100],
outputRange: [1, 1.5],
})
},
{ rotate: this.state.animation.interpolate({
inputRange: [0, 100],
outputRange: ['0 deg', '450 deg']})
}
]}
}
render() {
return (
<View style={{paddingTop: 40}}>
<Text style={{alignSelf: 'center', fontWeight: 'bold', marginBottom: 10}}>Custom animation</Text>
<SortableGrid
blockTransitionDuration = { 400 }
activeBlockCenteringDuration = { 200 }
itemsPerRow = { 4 }
dragActivationTreshold = { 200 }
dragStartAnimation = { this.getDragStartAnimation() }
onDragRelease = { (itemOrder) => console.log("Drag was released, the blocks are in the following order: ", itemOrder) }
onDragStart = { this.startCustomAnimation }
ref = { 'SortableGrid' }
>
{
this.numbers.map( (letter, index) =>
<View
ref={ 'itemref_' + index }
key={ index }
style={[
styles.block,
{ backgroundColor: this.getColor() }
]}
>
<Text style={{color: 'white', fontSize: 45}}>{letter}</Text>
</View>
)
}
</SortableGrid>
</View>
)
}
}
const styles = StyleSheet.create({
block: {
flex: 1,
margin: 8,
borderRadius: 20,
justifyContent: 'center',
alignItems: 'center'
}
});