スワイプジェスチャーの方向を検出する必要がありますが、問題があります。ジェスチャーは機能していますが、方向を検出する方法がわかりません。 ...
swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)];
[swipeGesture setNumberOfTouchesRequired:1];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[appView addGestureRecognizer:swipeGesture];
-(void)detectSwipe:(UISwipeGestureRecognizer *)recognizer {
switch (recognizer.direction) {
case UISwipeGestureRecognizerDirectionUp:
NSLog(@"smth1");
break;
case UISwipeGestureRecognizerDirectionDown:
NSLog(@"smth2");
default:
break;
}
}
動いていない :/
direction
プロパティは、特定のスワイプのactual方向ではなく、スワイプとして認識されるallowed方向のみを定義します。
代わりに、2つの別個のジェスチャー認識機能を使用するのが最も簡単です。 locationInView:
メソッドを使用して、ジェスチャーが開始したときと終了したときのタッチの位置を調べることもできます。
これが私のプロジェクトの例です:
// ...
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUp];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeDown];
// ...
- (void)didSwipe:(UISwipeGestureRecognizer*)swipe{
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Swipe Left");
} else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Swipe Right");
} else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"Swipe Up");
} else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"Swipe Down");
}
}
Omzのソリューションの拡張:
_self.myView
_は、ジェスチャーレコグナイザーを配置するビューです。以下のコードはテストされていません。レコグナイザをproperty
sとして保持し、viewDidLoad()
またはxib
ファイル内に追加する方が良いでしょう。 self
はUIViewController
です。
_UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft ];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight ];
[self.view addGestureRecognizer:swipeRight];
_
これら2つのメソッドをUIViewController
に追加し、必要なアクションを追加します。
_- (IBAction)swipedRight:(UISwipeGestureRecognizer *)recognizer
{
NSLog(@"swiped right");
}
- (IBAction)swipedLeft:(UISwipeGestureRecognizer *)recognizer
{
NSLog(@"swiped left");
}
_
Swift 5でスワイプジェスチャの方向を検出
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
@objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
if (sender.direction == .left) {
print("Swipe Left")
}
if (sender.direction == .right) {
print("Swipe Right")
}
}