首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-native-swiper方法scrollBy

react-native-swiper方法scrollBy
EN

Stack Overflow用户
提问于 2017-04-04 15:44:42
回答 5查看 6.7K关注 0票数 0

如果单击索引0,则该方法不起作用。如果我点击索引0,1和2将停止工作。如果我在1或2上单击2次,则滑块将转到所需的幻灯片。但零指数根本不起作用。请告诉我,可能的问题是什么?

代码语言:javascript
复制
   <Swiper
        onMomentumScrollEnd={(e, state, context) => this.setState({index: 
        state.index})}
        ref={(swiper) => {this.swiper = swiper;}}
        showsButtons={false}
        width={500}
        height={500}
        showsPagination={false}
        index={0}
        loop={true} >
        <View>
            <Text>One</Text>
        </View>
        <View>
            <Text>Two</Text>
        </View>
        <View>
            <Text>Three</Text>
        </View>
    </Swiper>
    <Text onPress={()=>{this.swiper.scrollBy(0, true)}}>One</Text>
    <Text onPress={()=>{this.swiper.scrollBy(1, true)}}>Two</Text>
    <Text onPress={()=>{this.swiper.scrollBy(2, true)}}>Three</Text>
EN

回答 5

Stack Overflow用户

发布于 2017-04-04 18:53:58

我这样做的方法:

代码语言:javascript
复制
onClickScroll(index){
    let currentIndex = this.state.index;

    if(currentIndex !== index) {
        let resultSlide = undefined;
        let countSlides = this.state.itineraryDaysListItem.length;

        if(index > currentIndex && index !== countSlides)
        {
            resultSlide = index - currentIndex;
            this.swiper.scrollBy(resultSlide, true);
        }
        else if(index>currentIndex && index === countSlides)
        {
            resultSlide = currentIndex+1;
            this.swiper.scrollBy(resultSlide, true);
        }
        else if(index < currentIndex && index !== 0){
            resultSlide = (currentIndex - index) * (-1);
            this.swiper.scrollBy(resultSlide, true);
        }
        else if(index < currentIndex && index === 0){
            resultSlide = currentIndex * (-1);
            this.swiper.scrollBy(resultSlide, true);
        }
    }
}               
票数 3
EN

Stack Overflow用户

发布于 2017-04-04 16:25:21

下面的实现适用于我

代码语言:javascript
复制
    'use strict'
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

import Swiper from 'react-native-swiper';

var styles = StyleSheet.create({
  wrapper: {
  },
  slide1: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#9DD6EB',
  },
  slide2: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#97CAE5',
  },
  slide3: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#92BBD9',
  },
  text: {
    color: '#fff',
    fontSize: 30,
    fontWeight: 'bold',
  }
})
export default class swiper  extends Component{
  constructor (props) {
    super(props)
    this.swiperRef = swiper => this.swiper = swiper
    this.scrollHandler = page => {
      console.log ('Page ',page,this.swiper)
      this.swiper && this.swiper.scrollBy(page, true)
    }
  }
  render (){
    return (
      <Swiper
        ref={ this.swiperRef }
        showsButtons={false}
        width={500}
        height={500}
        showsPagination={false}
        index={0}
        loop={true} >
        <View style={ styles.slide1 }>
            <Text style={ styles.text } onPress={()=>{console.log('Page 0'); this.swiper.scrollBy(1, true)}}>One</Text>
        </View>
        <View style={ styles.slide2 }>
             <Text style={ styles.text } onPress={()=>{console.log('Page 1'); this.swiper.scrollBy(1, true)}}>Two</Text>
        </View>
        <View style={ styles.slide3 } >
            <Text style={ styles.text } onPress={()=>{console.log('Page 2');this.swiper.scrollBy(2, true)}}>Three</Text>
        </View>
    </Swiper>
    )
  }
}


AppRegistry.registerComponent('myproject', () => swiper);
票数 2
EN

Stack Overflow用户

发布于 2019-10-29 12:13:34

对于在2019年阅读这篇文章并使用函数组件/挂钩编写的任何人来说。

代码语言:javascript
复制
import React, { useRef } from 'react'
import Swiper from 'react-native-swiper'

const YourCoolComponent = () => {
  const swiper = useRef(null)
  return (
    <Swiper ref={swiper}>
      <View>
        <Text>Page 1</Text>
        <Button onPress={() => swiper.current.scrollBy(1)}>Forward</Button>
      </View>
      <View>
        <Text>Page 2</Text>
        <Button onPress={() => swiper.current.scrollBy(-1)}>Backward</Button>
      </View>
    </Swiper>
  )
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43201191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档