Skip to content

Commit d8365ce

Browse files
committed
Fixes double added delta in drag behavior
1 parent 7e06fb8 commit d8365ce

10 files changed

Lines changed: 56 additions & 34 deletions

File tree

playground/src/interactable/Animator.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ export default class PhysicsAnimator {
3131
this.animatorListener.onAnimationFrame();
3232
requestAnimationFrame( () => this.doFrame( Date.now() ) )
3333
}
34+
35+
debugStart( behaviour ){
36+
if( !this.debug ) return;
37+
this.debugB = behaviour
38+
this.debugInitialV = {...this.physicsObject}
39+
}
40+
41+
debugEnd(){
42+
if( !this.debug ) return;
43+
console.log( `Debug ${this.debugB.type}`, {
44+
dvx: this.physicsObject.vx - this.debugInitialV.vx,
45+
dvy: this.physicsObject.vy - this.debugInitialV.vy,
46+
})
47+
}
3448

3549
animateFrameWithDeltaTime( deltaTime ) {
3650
if( !deltaTime ) return;
@@ -40,7 +54,9 @@ export default class PhysicsAnimator {
4054
let coords = View.getTranslation()
4155

4256
behaviors.forEach( behavior => {
57+
this.debugStart( behavior )
4358
Behaviors[ behavior.type ].doFrame( behavior, deltaTime, physicsObject, coords, View )
59+
this.debugEnd()
4460
})
4561

4662
let dx = 0;

playground/src/interactable/InteractableView.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ export default function injectDependencies( Animated, PanResponder ){
5555
constructor(props) {
5656
super(props)
5757

58+
let {x = 0, y = 0} = props.initialPosition
59+
5860
// In case animatedValueXY is not given
59-
this.animated = new Animated.ValueXY(0,0)
61+
this.animated = new Animated.ValueXY({x, y})
6062

6163
// This guy will apply all the physics
6264
this.animator = this.createAnimator( props )
65+
this.animator.debug = false
6366

6467
// Cache when the view is inside of an alert area
6568
this.insideAlertAreas = {}
@@ -76,9 +79,11 @@ export default function injectDependencies( Animated, PanResponder ){
7679
this.setPropBehaviours( {}, props )
7780

7881
// Set initial position
79-
let {x,y} = this.getAnimated( props )
80-
x.setValue( props.initialPosition.x )
81-
y.setValue( props.initialPosition.y )
82+
let animated = this.getAnimated( props )
83+
animated.x.setValue( x )
84+
animated.y.setValue( y )
85+
animated.x._startingValue = x
86+
animated.x._startingValue = y
8287

8388
// Save the last animation end position to report good coordinates in the events
8489
this.lastEnd = {...this.initialPosition}
@@ -141,7 +146,7 @@ export default function injectDependencies( Animated, PanResponder ){
141146
animate( dx, dy ){
142147
if(!dx && !dy) return
143148
let animated = this.getAnimated()
144-
console.log( dx + animated.x._value + animated.x._offset )
149+
console.log( dx + animated.x._value + animated.x._offset, this.animator.physicsObject )
145150

146151
let {x,y} = this.getTranslation()
147152
this.setTranslation( x + dx, y + dy )
@@ -228,11 +233,10 @@ export default function injectDependencies( Animated, PanResponder ){
228233
}
229234

230235
onDragging({dx, dy}){
231-
let pos = this.getTranslation()
232-
let x = dx + pos.x
233-
let y = dy + pos.y
234-
235-
console.log( this.dragBoundaries.minPoint )
236+
let x = dx + this.lastEnd.x
237+
let y = dy + this.lastEnd.y
238+
239+
console.log('diff', dx, dy)
236240

237241
let {minPoint, maxPoint} = this.dragBoundaries
238242
if( !this.props.verticalOnly ){
@@ -333,15 +337,11 @@ export default function injectDependencies( Animated, PanResponder ){
333337
}
334338
}
335339

336-
setInitialPosition( initialPosition ) {
337-
this.initialPosition = initialPosition;
338-
this.setTranslation( initialPosition )
339-
}
340-
341340
setVelocity( velocity ) {
342341
if ( this.dragBehavior ) return;
343-
this.animator.vx = velocity.x
344-
this.animator.vy = velocity.y
342+
let { physicsObject } = this.animator
343+
physicsObject.vx = velocity.x
344+
physicsObject.vy = velocity.y
345345
this.endDrag();
346346
}
347347

@@ -367,7 +367,7 @@ export default function injectDependencies( Animated, PanResponder ){
367367
changePosition( position ) {
368368
if ( this.dragBehavior ) return;
369369

370-
this.setTranslation( position )
370+
this.setTranslation( position.x, position.y )
371371
this.endDrag();
372372
}
373373

playground/src/real-life-examples/CollapsibleCalendar.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import Interactable from 'react-native-interactable';
44

55
const Screen = Dimensions.get('window');
66
const Calendar = {
7-
width: Screen.width - 16,
8-
height: (Screen.width -16) / 944 * 793
7+
width: 480 - 16,
8+
height: (480 -16) / 944 * 793
99
}
1010

1111
export default class CollapsibleFilter extends Component {
@@ -98,14 +98,14 @@ const styles = StyleSheet.create({
9898
},
9999
top: {
100100
backgroundColor: 'white',
101-
width: Screen.width,
101+
width: Calendar.width,
102102
alignItems: 'center',
103103
zIndex: 1000
104104
},
105105
header: {
106106
marginTop: 15,
107107
height: 40,
108-
width: Screen.width,
108+
width: Calendar.width,
109109
paddingLeft: 20
110110
},
111111
month: {
@@ -116,8 +116,8 @@ const styles = StyleSheet.create({
116116
fontWeight: 'bold'
117117
},
118118
days: {
119-
width: Screen.width - 16,
120-
height: (Screen.width -16) / 944 * 65
119+
width: Calendar.width - 16,
120+
height: (Calendar.width -16) / 944 * 65
121121
},
122122
calendar: {
123123
width: Calendar.width,
@@ -128,7 +128,7 @@ const styles = StyleSheet.create({
128128
},
129129
row: {
130130
flexDirection: 'row',
131-
width: Screen.width,
131+
width: Calendar.width,
132132
borderBottomWidth: 1,
133133
borderColor: '#eeeeee',
134134
height: 80,

playground/src/real-life-examples/CollapsibleFilter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const styles = StyleSheet.create({
147147
color: 'white'
148148
},
149149
photo: {
150-
width: Screen.width-40,
150+
width: 480-40,
151151
height: 190,
152152
marginBottom: 20
153153
}

playground/src/real-life-examples/MapPanel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { StyleSheet, View, Dimensions, Image, Text, Animated, TouchableOpacity }
33
import Interactable from 'react-native-interactable';
44

55
const Screen = {
6-
width: Dimensions.get('window').width,
6+
width: 480,
77
height: Dimensions.get('window').height - 75
88
}
99

playground/src/real-life-examples/NotifPanel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { StyleSheet, View, Dimensions, Image, Text, Platform } from 'react-nativ
33
import Interactable from 'react-native-interactable';
44

55
const Screen = {
6-
width: Dimensions.get('window').width,
6+
width: 480,
77
height: Dimensions.get('window').height - 75
88
}
99

playground/src/real-life-examples/NowCard.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const styles = StyleSheet.create({
8080
backgroundColor: '#efefef',
8181
},
8282
card: {
83-
width: Screen.width - 40,
83+
width: 480 - 40,
8484
backgroundColor: 'white',
8585
borderRadius: 6,
8686
marginHorizontal: 20,
@@ -92,7 +92,7 @@ const styles = StyleSheet.create({
9292
elevation: 4
9393
},
9494
image: {
95-
width: Screen.width - 40,
95+
width: 480 - 40,
9696
height: Screen.height <= 500 ? 70 : 150
9797
},
9898
header: {
@@ -118,7 +118,7 @@ const styles = StyleSheet.create({
118118
playground: {
119119
marginTop: Screen.height <= 500 ? 10 : 40,
120120
padding: 20,
121-
width: Screen.width - 40,
121+
width: 480 - 40,
122122
backgroundColor: '#459FED',
123123
alignItems: 'stretch'
124124
},

playground/src/real-life-examples/RowActions1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ const styles = StyleSheet.create({
240240
playground: {
241241
marginTop: Screen.height <= 500 ? 0 : 80,
242242
padding: 20,
243-
width: Screen.width - 40,
243+
width: 480 - 40,
244244
backgroundColor: '#459FED',
245245
alignItems: 'stretch',
246246
alignSelf: 'center'

playground/src/real-life-examples/RowActions2.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import React, { Component } from 'react';
22
import { StyleSheet, View, Image, Text, Animated, TouchableOpacity, Dimensions, Slider } from 'react-native';
33
import Interactable from 'react-native-interactable';
44

5-
const Screen = Dimensions.get('window');
5+
const Screen = {
6+
width: 480,
7+
height: Dimensions.get('window')
8+
}
69

710
export default class RowActions2 extends Component {
811
constructor(props) {

playground/src/real-life-examples/TinderCard.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import React, { Component } from 'react';
22
import { StyleSheet, View, Dimensions, Image, Text, Animated } from 'react-native';
33
import Interactable from 'react-native-interactable';
44

5-
const Screen = Dimensions.get('window');
5+
const Screen = {
6+
width: 480,
7+
height: Dimensions.get('window').height
8+
}
69

710
export default class TinderCard extends Component {
811
constructor(props) {

0 commit comments

Comments
 (0)