LoginSignup
4
4

More than 5 years have passed since last update.

Rubymotionでカルーセルを実装

Last updated at Posted at 2014-05-19

作っているアプリで写真を表示する部分があり、カルーセルを実装したいと思ったのでその時のメモ。

1.iCarouselのインストール

cocoapods( http://cocoapods.org/ )で調べた限りiCarouselが一番良さそう。

Motion::Project::App.setup do |app|
  app.pods do
    pod 'iCarousel'
  end
end
rake pod:install

2.iCarouselの実装

※iCarouselは色々なプロパティやメソッドを持っているので詳しくはgithubを確認してください。
https://github.com/nicklockwood/iCarousel

app/controllers/hoge_controller.rb

def viewDidLoad
...

@items=[]
#photo1,photo2,photo3はUIImageViewにUIImageをセットしたもの。
@items << photo1
@items << photo2
@items << photo3

#ICarouselのサイズを設定
carousel= ICarousel.alloc.initWithFrame(CGRectMake( 0, 120, self.view.frame.size.width, 100 ))
#ICarouselのオプションを設定
carousel.setBackgroundColor(UIColor.clearColor)
carousel.dataSource = self
carousel.delegate = self
carousel.decelerationRate = 0.6
#iCarouselは色々な種類のカルーセルを実装できる。
carousel.type = ICarouselTypeLinear

self.view.addSubview(carousel)

end
app/controllers/hoge_controller.rb

def carousel(carousel, viewForItemAtIndex: index, reusingView: view)
  #画像の下に表示するlabelの設定
  label = nil (※特に表示していないのでnilにしてます)
  view = @items[index] unless view
  return view
end

def carousel(carousel, valueForOption: option, withDefault: value)
  #carousel間のスペースを設定
  return value * 1.00 if option == ICarouselOptionSpacing
  return value
end

少してこずりましたが、簡単に実装できました。

4
4
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
4
4