diff --git a/src/index.js b/src/index.js index 6a3c628e..9664409b 100644 --- a/src/index.js +++ b/src/index.js @@ -503,6 +503,47 @@ export default class extends Component { } } + /** + * Scroll to index + * @param {number} index page + * @param {bool} animated + */ + + scrollTo = (index, animated = true) => { + if (this.internals.isScrolling || this.state.total < 2 || index == this.state.index) return + + const state = this.state + const diff = this.state.index + (index - this.state.index) + + let x = 0 + let y = 0 + if (state.dir === 'x') x = diff * state.width + if (state.dir === 'y') y = diff * state.height + + if (Platform.OS !== 'ios') { + this.scrollView && this.scrollView[animated ? 'setPage' : 'setPageWithoutAnimation'](diff) + } else { + this.scrollView && this.scrollView.scrollTo({ x, y, animated }) + } + + // update scroll state + this.internals.isScrolling = true + this.setState({ + autoplayEnd: false + }) + + // trigger onScrollEnd manually in android + if (!animated || Platform.OS !== 'ios') { + setImmediate(() => { + this.onScrollEnd({ + nativeEvent: { + position: diff + } + }) + }) + } + } + scrollViewPropOverrides = () => { const props = this.props let overrides = {}