Skip to content

Commit

Permalink
Merge pull request #2 from alattanzio/alattanzio-patch-2
Browse files Browse the repository at this point in the history
Added scrollTo
  • Loading branch information
alattanzio authored Jul 17, 2018
2 parents a1e052e + 64fdd5c commit be1b3be
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ const styles = {

buttonText: {
fontSize: 50,
color: '#007aff'
color: '#007aff',
fontFamily: 'Arial'
}
}

Expand Down Expand Up @@ -485,6 +486,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 = {}
Expand Down

0 comments on commit be1b3be

Please sign in to comment.