2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Xamarin.iOS で、スワイプで View を切り替える方法

Last updated at Posted at 2013-07-23

いろんなアプリで使われてるからもっと簡単なのかと思ってましたよ…。

ここら辺りを参考に Xamarin.iOS で実装してみた。

Xamarin.iOS プロジェクトを作って、InterfaceBuilder で UIScrollView を配置する

ほぼ上記サイト通りなので割愛。
中でも Paging Enabled を ON にしておくのが重要。
UIScrollView は Outlet で変数にしておく。

プロジェクトに画像を追加する

image1.png, image2.png, image3.png をプロジェクトに追加。ビルドアクションを Content にする。

画像のサイズは w:320px, h:460px にしました。

一応おいておきます → http://blog.amay077.net/assets/images/posts/swipe_view_images.zip

実装する

Android の Gelleley みたく、「画像を追加するだけであとはよろしくやってくれる」もんだと思ってたんですが、ほぼなにもやってくれない のですね。
自力で座標をずらして追加する、ってゴリゴリじゃないですか。

SwipeSampleViewController.cs
public override void ViewDidLoad()
{
    base.ViewDidLoad();

    SizeF imageSize = new SizeF(320f, 460f);
    for (int i = 0; i < 3; i++)
    {
        var image = UIImage.FromFile(String.Format("image{0}.png", i));

        var imgView = new UIImageView(image);
        // x方向にずらしながら…
        imgView.Frame = new RectangleF(imageSize.Width * i, 0f, 
                                       imageSize.Width, imageSize.Height);
        // 子として追加。
        scrollView.AddSubview(imgView);
    }
    // スクロール可能領域を設定する
    scrollView.ContentSize = new SizeF(imageSize.Width * 3, imageSize.Height);
}

なんか参考サイトのコードよりずいぶんすっきりしたぞ。

imgView の破棄とかやってないので、ちゃんと使う時はちゃんと破棄しましょう。(ViewDidUnload が Deprecated なようなのだけど、どこで破棄したら?Dispose?)

その他

  • 「ページが変わった時のイベント」があるかと思ったらなかった、これも自力実装かorz
  • 「無限スクロール」みたいなのやりたいけどこのアプローチじゃない気がする
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?