4
4

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 でジェスチャを認識する

Last updated at Posted at 2013-07-27

Xamarin.iOS でも UIGestureRecognizer が普通に使えるわけですが、Objective-C に比べてコードが短く書けて感動した話です。

本日の Obj-C の先生はこちら。

このサンプルを Xamarin.iOS に移植してみます。

サンプルコード

GesturesSample_ViewDidLoad.cs
// Tap gesture
this.View.AddGestureRecognizer(new UITapGestureRecognizer(tap => 
{
    Debug.WriteLine("Double Tap.");
}) 
{ 
    NumberOfTapsRequired = 2 // Double tap 
});

// Drag(Pan) gesture
this.View.AddGestureRecognizer(new UIPanGestureRecognizer(pan => 
{
    var p = pan.TranslationInView(this.View);
    var v = pan.VelocityInView(this.View);
    Debug.WriteLine("Pan. transration:{0}, velocity:{1}", p, v);
}));

// Pinch gesture
this.View.AddGestureRecognizer(new UIPinchGestureRecognizer(pin => 
{
    var scale = pin.Scale;
    var v = pin.Velocity;
    Debug.WriteLine("Pinch. scale:{0}, velocity:{1}", scale, v);
}));

// Swipe gesture
this.View.AddGestureRecognizer(new UISwipeGestureRecognizer(sw => 
{
    Debug.WriteLine("Swipe.");
}));

// Rotate gesture
this.View.AddGestureRecognizer(new UIRotationGestureRecognizer(ro => 
{
    var rotation = ro.Rotation;
    var v = ro.Velocity;
    Debug.WriteLine("Rotate. rotation:{0}, velocity:{1}", rotation, v);
}));

// Long press gesture
this.View.AddGestureRecognizer(new UILongPressGestureRecognizer(lp => 
{
    Debug.WriteLine("Long press.");
}));

ViewController 全体のソースは コチラ

元のサイトのサンプルコードは 70行弱ありますが、Xamarin.iOS では 45行くらいで書けました。しかも、GestureRecongnizer の登録とハンドラが同じ場所に書けるので見やすい。

しかしこれ、ハンドラとか GesutureRecognizer、破棄しなくていいのかなあ。。。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?