LoginSignup
7
5

More than 5 years have passed since last update.

react-native-snap-carouselの基本

Last updated at Posted at 2017-12-27

alt

react-native-snap-carouselはreact-nativeにおけるcarouselの決定版だ(今のところ)
自作した方が柔軟性は高いが、、、
基本的な使い方は以下のような感じ

import React, { PureComponent } from "react";
import { Dimensions, StyleSheet, View } from "react-native";

import Carousel from "react-native-snap-carousel";

export default class ExampleCarousel extends PureComponent {
  state = {
    data: [{}, {}, {}, {}, {}, {}]
  };

  renderItem = () => <View style={styles.tile} />;

  render() {
    return (
      <View style={styles.container}>
        <Carousel
          data={this.state.data}
          renderItem={this.renderItem}
          itemWidth={Dimensions.get("window").width * 0.85}
          sliderWidth={Dimensions.get("window").width}
          containerCustomStyle={styles.carousel}
          slideStyle={{ flex: 1 }}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    height: 300
  },
  carousel: {
    flex: 1,
    backgroundColor: "red"
  },
  tile: {
    flex: 1,
    width: Dimensions.get("window").width * 0.85,
    backgroundColor: "yellow"
  }
});

こんな感じでスワイプできる写真が簡単に出来る
スクリーンショット 2017-12-27 17.38.44.png

TouchableOcacityを絡めたい時は、

renderItem = () => <View style={styles.tile} />;

  _onPressCarousel = () => {
    // here handle carousel press
  };
  renderItem = () => (
    <TouchableOpacity onPress={this._onPressCarousel} style={styles.tile}>
      <Image style={styles.button} source={require("../img/Sample.jpg")} />
    </TouchableOpacity>
  );

上の様にに書き換えればok
いいね欲しいです

loopでループさせることが出来、
autoplayで、自動再生出来る。
enableMomentumで指を離した時、ゆっくりスライドを元の位置に戻すようになる。
これは横移動に時間がかかるのでおすすめしない。

      <Carousel
        data={this.state.data}
        renderItem={this.renderItem}
        itemWidth={width * 0.85}
        sliderWidth={width}
        containerCustomStyle={styles.carousel}
        slideStyle={{ flex: 1 }}
        keyExtractor={this._keyExtractor}
        loop
        autoplay
      />

parallaxImageを使うと、立体的に見せてくれるそうですが、
まだ使い方が分からないw

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5