Skip to content

Instantly share code, notes, and snippets.

@bogoslavskiy
Last active July 8, 2018 19:34
Show Gist options
  • Save bogoslavskiy/2cb76890c7dfa88ba46d4852b4cde28b to your computer and use it in GitHub Desktop.
Save bogoslavskiy/2cb76890c7dfa88ba46d4852b4cde28b to your computer and use it in GitHub Desktop.
constructor(initialState) {
this.initialState = initialState;
this._createClampedScroll();
this.scrollY.addListener(this._updateScroll);
}
_updateScroll = ({value}) => {
const diff = value - this._scrollValue;
this._scrollValue = Math.max(value, this.topPartHeight); // Fix normal state
this._clampedScrollValue = Math.min(
Math.max(this._clampedScrollValue + diff, this.minClamp),
this.maxClamp
);
this._changeStatusBarStyle();
this._changeStateBar();
};
_createClampedScroll() {
this.clampedScroll = Animated.diffClamp(
this.scrollY.interpolate({ // Only positive
inputRange: [0, 1],
outputRange: [0, 1],
extrapolateLeft: 'clamp',
}).interpolate({ // Fix normal state
inputRange: [0, this.topPartHeight],
outputRange: [this.topPartHeight, this.topPartHeight],
extrapolate: 'identity',
}),
this.minClamp,
this.maxClamp,
);
}
_changeStateBar() {
let newState, types = this.stateBarTypes;
let clampedValue = Math.round(this._clampedScrollValue);
if(Math.round(this.scrollY._value) < this.topPartHeight) {
newState = types.EXPANDED;
} else if(clampedValue == this.minClamp) {
newState = types.NORMAL;
} else if(clampedValue == this.maxClamp) {
newState = types.CLAMPED;
}
if(newState != undefined && newState != this.stateBar) {
this.stateBar = newState;
}
}
_changeStatusBarStyle() {
let statusBarStyle = Math.round(this._clampedScrollValue) != this.maxClamp ?
'light-content' :
'dark-content';
if(statusBarStyle != this._statusBarStyle) {
StatusBar.setBarStyle(statusBarStyle);
this._statusBarStyle = statusBarStyle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment