スワイプジェスチャーの方向をどのように検出しますか?
-
26-10-2019 - |
質問
スワイプジェスチャーの方向を検出する必要がありますが、問題があります。ジェスチャーは機能していますが、方向を検出する方法がわかりません。 ...
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
プロパティはのみを定義します 許可された スワイプとして認識されている方向ではなく、スワイプとして認識されます 実際 特定のスワイプの方向。
最も簡単なのは、代わりに2つの個別のジェスチャー認識者を使用することです。また、ジェスチャーが始まったときとそれが終わるときにタッチの位置を検査することもできます locationInView:
方法。
他のヒント
これが私のプロジェクトの1つの例です。
// ...
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
aです 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")
}
}
所属していません StackOverflow