-
Notifications
You must be signed in to change notification settings - Fork 735
Feat/panning fixes #483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/panning fixes #483
Changes from 2 commits
8bff82d
aa61ba7
a2faffd
b6ebc07
0ef695c
cf2bffe
3fd5364
8f53572
20b4a14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import PanningProvider from './panningProvider'; | |
const DEFAULT_SPEED = 20; | ||
const DEFAULT_BOUNCINESS = 6; | ||
const DEFAULT_DISMISS_ANIMATION_DURATION = 280; | ||
const DEFAULT_MAXIMUM_DRAGS_AFTER_SWIPE = 1; | ||
|
||
/** | ||
* @description: PanDismissibleView component created to making listening to swipe and drag events easier, | ||
|
@@ -37,6 +38,10 @@ class PanDismissibleView extends PureComponent { | |
bounciness: PropTypes.number, | ||
duration: PropTypes.number, | ||
}), | ||
/** | ||
* The maximum number of swipes that can occur after a swipe before resetting the swipe (default is 1) | ||
*/ | ||
maximumDragsAfterSwipe: PropTypes.number, | ||
}; | ||
|
||
static defaultProps = { | ||
|
@@ -51,6 +56,7 @@ class PanDismissibleView extends PureComponent { | |
bounciness: DEFAULT_BOUNCINESS, | ||
duration: DEFAULT_DISMISS_ANIMATION_DURATION, | ||
}, | ||
maximumDragsAfterSwipe: DEFAULT_MAXIMUM_DRAGS_AFTER_SWIPE, | ||
onDismiss: _.noop, | ||
}; | ||
|
||
|
@@ -61,6 +67,7 @@ class PanDismissibleView extends PureComponent { | |
animTranslateX: new Animated.Value(0), | ||
animTranslateY: new Animated.Value(0), | ||
isAnimating: false, | ||
forceDismissIsPending: false | ||
}; | ||
this.ref = React.createRef(); | ||
} | ||
|
@@ -110,22 +117,32 @@ class PanDismissibleView extends PureComponent { | |
} | ||
}; | ||
|
||
initPositions = () => { | ||
initPositions = (extraDataForSetState, runAfterSetState) => { | ||
this.setNativeProps(0, 0); | ||
this.setState({ | ||
animTranslateX: new Animated.Value(0), | ||
animTranslateY: new Animated.Value(0), | ||
}); | ||
...extraDataForSetState | ||
}, runAfterSetState); | ||
}; | ||
|
||
onPanStart = () => { | ||
this.swipe = {}; | ||
this.counter = 0; | ||
}; | ||
|
||
onDrag = deltas => { | ||
const {maximumDragsAfterSwipe} = this.props; | ||
const left = deltas.x ? Math.round(deltas.x) : 0; | ||
const top = deltas.y ? Math.round(deltas.y) : 0; | ||
this.setNativeProps(left, top); | ||
if (this.swipe.x || this.swipe.y) { | ||
if (this.counter < maximumDragsAfterSwipe) { | ||
this.counter += 1; | ||
} else { | ||
this.swipe = {}; | ||
} | ||
} | ||
}; | ||
|
||
setNativeProps = (left, top) => { | ||
|
@@ -193,8 +210,9 @@ class PanDismissibleView extends PureComponent { | |
}; | ||
|
||
onInitAnimationFinished = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
this.setState({isAnimating: false}); | ||
this.initPositions(); | ||
const {forceDismissIsPending} = this.state; | ||
const runAfterSetState = forceDismissIsPending ? this.animateDismiss : undefined; | ||
this.initPositions({isAnimating: false, forceDismissIsPending: false}, runAfterSetState); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need it in the state (but I removed |
||
}; | ||
|
||
getDismissAnimationDirection = () => { | ||
|
@@ -232,14 +250,19 @@ class PanDismissibleView extends PureComponent { | |
// isDown === true --> animate to the bottom | ||
// isDown === false --> animate to the top | ||
animateDismiss = () => { | ||
const {directions = []} = this.props; | ||
const hasUp = directions.includes(PanningProvider.Directions.UP); | ||
const hasRight = directions.includes(PanningProvider.Directions.RIGHT); | ||
const hasLeft = directions.includes(PanningProvider.Directions.LEFT); | ||
const hasDown = !hasUp && !hasLeft && !hasRight; // default | ||
const verticalDismiss = hasDown ? true : hasUp ? false : undefined; | ||
const horizontalDismiss = hasRight ? true : hasLeft ? false : undefined; | ||
this._animateDismiss(horizontalDismiss, verticalDismiss); | ||
const {isAnimating} = this.state; | ||
if (isAnimating) { | ||
this.setState({forceDismissIsPending: true}); | ||
} else { | ||
const {directions = []} = this.props; | ||
const hasUp = directions.includes(PanningProvider.Directions.UP); | ||
const hasRight = directions.includes(PanningProvider.Directions.RIGHT); | ||
const hasLeft = directions.includes(PanningProvider.Directions.LEFT); | ||
const hasDown = !hasUp && !hasLeft && !hasRight; // default | ||
const verticalDismiss = hasDown ? true : hasUp ? false : undefined; | ||
const horizontalDismiss = hasRight ? true : hasLeft ? false : undefined; | ||
this._animateDismiss(horizontalDismiss, verticalDismiss); | ||
} | ||
}; | ||
|
||
_animateDismiss = (isRight, isDown) => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.