Coder Social home page Coder Social logo

Snapping about rn-sliding-up-panel HOT 7 CLOSED

octopitus avatar octopitus commented on July 17, 2024 6
Snapping

from rn-sliding-up-panel.

Comments (7)

jsainz237 avatar jsainz237 commented on July 17, 2024 2

@epurban

I created breaks by getting rid of the flick animation and using pure PanResponder and Animation.
(EDIT: For some reason it doesn't like to format my code correctly, so some code may appear out of the code box)

Inside the _onPanResponderRelease(evt, gestureState) function I did the following.
`

//gesture state has negative values for up, so these are less confusing
this.correctedDY = -gestureState.dy;
this.correctedVY = -gestureState.vy;

// BOTTOM PEEK
if(this.currentPosition === this.bottomPeek) {
  //user drags/slides up
  if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY > 0 ) || this.correctedDY > 50) {
    
    //if user drags over mid break point, go to topbreak
    if(this.correctedDY > (this.midBreak - this.bottomPeek + 20)) {
      this.transitionTo(this.topBreak, gestureState.vy);
    }
    else {
      this.transitionTo(this.midBreak, gestureState.vy);
    }
  }

  //if user drags/slides down
  else if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY < 0 ) || this.correctedDY < -25 ) {
    
    this.transitionTo(this.hidden, gestureState.vy);
    
    //user if closing panel...emit close panel event
    EventRegister.emit('closePanel');
  }
  else {
    this.transitionTo(this.bottomPeek, gestureState.vy);
  }
}

// MID BREAK
else if(this.currentPosition === this.midBreak) {
  //user drags/slides up
  if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY > 0 ) || this.correctedDY > 70) {
    this.transitionTo(this.topBreak, gestureState.vy);
  }
  //user drags/slides down
  else if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY < 0 ) || this.correctedDY < -50) {
    this.transitionTo(this.bottomPeek, gestureState.vy);
  }
  else {
    this.transitionTo(this.midBreak, gestureState.vy);
  }
}

// TOP BREAK
else if(this.currentPosition === this.topBreak) {
  //if user slides down
  if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY < 0) || this.correctedDY < -50) {
    
    //if user drags over mid break point, go to bottomPeak
    if(this.correctedDY < -(this.topBreak - this.midBreak + 20)) {
      this.transitionTo(this.bottomPeek, gestureState.vy);
    }
    else {
      this.transitionTo(this.midBreak, gestureState.vy);
    }
  }
  else {
    this.transitionTo(this.topBreak);
  }
}

`
topBreak, midBreak, bottomPeek, and Hidden are just values the slider should transition to

I also changed the transitionTo function slightly to be able to have a initial velocity and to be able to use the Spring animation

`transitionTo(toValue, initialVelocity = 0) {
this.currentPosition = toValue;
return this._triggerAnimation(toValue, initialVelocity)
}

_triggerAnimation(toValue, initialVelocity) {

//if going to topBreak, clamp the animation
const clamping = toValue === this.topBreak ? true : false
const onAnimationEnd = () => {}

const animationConfig = {
  bounciness: 10,
  velocity: initialVelocity,
  overshootClamping: clamping,
  toValue: -toValue,
}

const animation = Animated.spring(
  this._translateYAnimation,
  animationConfig,
)

animation.start(onAnimationEnd)

}`

this.currentPosition is just a variable I use to track where the panel currently is

from rn-sliding-up-panel.

octopitus avatar octopitus commented on July 17, 2024

Thanks for your suggestion @AustinErck. Yes I've thought about it for a while. Will leave this issue open as a reference.

from rn-sliding-up-panel.

prathammehta avatar prathammehta commented on July 17, 2024

It'd be brilliant if this feature was added.

from rn-sliding-up-panel.

epurban avatar epurban commented on July 17, 2024

Has anyone created this feature?

from rn-sliding-up-panel.

epurban avatar epurban commented on July 17, 2024

@epurban

I created breaks by getting rid of the flick animation and using pure PanResponder and Animation.
(EDIT: For some reason it doesn't like to format my code correctly, so some code may appear out of the code box)

Inside the _onPanResponderRelease(evt, gestureState) function I did the following.
`

//gesture state has negative values for up, so these are less confusing
this.correctedDY = -gestureState.dy;
this.correctedVY = -gestureState.vy;

// BOTTOM PEEK
if(this.currentPosition === this.bottomPeek) {
  //user drags/slides up
  if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY > 0 ) || this.correctedDY > 50) {
    
    //if user drags over mid break point, go to topbreak
    if(this.correctedDY > (this.midBreak - this.bottomPeek + 20)) {
      this.transitionTo(this.topBreak, gestureState.vy);
    }
    else {
      this.transitionTo(this.midBreak, gestureState.vy);
    }
  }

  //if user drags/slides down
  else if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY < 0 ) || this.correctedDY < -25 ) {
    
    this.transitionTo(this.hidden, gestureState.vy);
    
    //user if closing panel...emit close panel event
    EventRegister.emit('closePanel');
  }
  else {
    this.transitionTo(this.bottomPeek, gestureState.vy);
  }
}

// MID BREAK
else if(this.currentPosition === this.midBreak) {
  //user drags/slides up
  if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY > 0 ) || this.correctedDY > 70) {
    this.transitionTo(this.topBreak, gestureState.vy);
  }
  //user drags/slides down
  else if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY < 0 ) || this.correctedDY < -50) {
    this.transitionTo(this.bottomPeek, gestureState.vy);
  }
  else {
    this.transitionTo(this.midBreak, gestureState.vy);
  }
}

// TOP BREAK
else if(this.currentPosition === this.topBreak) {
  //if user slides down
  if( (Math.abs(gestureState.vy) > MINIMUM_VELOCITY_THRESHOLD && this.correctedVY < 0) || this.correctedDY < -50) {
    
    //if user drags over mid break point, go to bottomPeak
    if(this.correctedDY < -(this.topBreak - this.midBreak + 20)) {
      this.transitionTo(this.bottomPeek, gestureState.vy);
    }
    else {
      this.transitionTo(this.midBreak, gestureState.vy);
    }
  }
  else {
    this.transitionTo(this.topBreak);
  }
}

`
topBreak, midBreak, bottomPeek, and Hidden are just values the slider should transition to

I also changed the transitionTo function slightly to be able to have a initial velocity and to be able to use the Spring animation

`transitionTo(toValue, initialVelocity = 0) {
this.currentPosition = toValue;
return this._triggerAnimation(toValue, initialVelocity)
}

_triggerAnimation(toValue, initialVelocity) {

//if going to topBreak, clamp the animation
const clamping = toValue === this.topBreak ? true : false
const onAnimationEnd = () => {}

const animationConfig = {
  bounciness: 10,
  velocity: initialVelocity,
  overshootClamping: clamping,
  toValue: -toValue,
}

const animation = Animated.spring(
  this._translateYAnimation,
  animationConfig,
)

animation.start(onAnimationEnd)

}`

this.currentPosition is just a variable I use to track where the panel currently is

Thank you for this. It has helped my project save some time :)

from rn-sliding-up-panel.

epurban avatar epurban commented on July 17, 2024

Does anyone have time to update this to work with version 2.0?

from rn-sliding-up-panel.

Eralyne avatar Eralyne commented on July 17, 2024

@epurban I've implemented a snapping method shown in my issue that I recently closed, if you still need it. #102

from rn-sliding-up-panel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.