Жесты iPhone, добавляющие 2 сразу
Задача С ответы тоже хорошо. Это в C# Monotouch.
В настоящее время я использую этот код для добавления 2 жестов (влево / вправо) в мой WebView. Работает отлично.
Можно ли объединить это в меньший код, чтобы показать, что оба жеста выполняют одно и то же действие?
//LEFT
UISwipeGestureRecognizer sgr = new UISwipeGestureRecognizer ();
sgr.AddTarget (this, MainViewController.MySelector);
sgr.Direction = UISwipeGestureRecognizerDirection.Left;
sgr.Delegate = new SwipeRecognizerDelegate ();
this.View.AddGestureRecognizer (sgr);
//RIGHT
UISwipeGestureRecognizer sgrRight = new UISwipeGestureRecognizer ();
sgrRight.AddTarget (this, MainViewController.MySelector);
sgrRight.Direction = UISwipeGestureRecognizerDirection.Right;
sgrRight.Delegate = new SwipeRecognizerDelegate ();
this.View.AddGestureRecognizer (sgrRight);
2 ответа
Решение
Вы можете попробовать эту версию, которая короче и использует преимущества C# lambdas:
UISwipeGestureRecognizer sgr = new UISwipeGestureRecognizer ();
sgr.Action = delegate {
// Your handler goes here
});
sgr.Direction = UISwipeGestureRecognizerDirection.Left |
UISwipeGestureRecognizerDirection.Right;
this.View.AddGestureRecognizer (sgr);
В ObjC вы бы использовали побитовый или оператор с указаниями, чтобы включить их обоих. Но что касается любого языка, который вы используете, вам повезет получить большую поддержку от любого опытного разработчика iOS.