[英]How to detect end of UISwipeGestureRecognizer?
From the Apple documentation 来自Apple文档
A swipe is a discrete gesture, and thus the associated action message is sent only once per gesture. 滑动是离散手势,因此每个手势仅发送一次相关联的动作消息。
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event
also doesn't gets called when I use UISwipeGestureRecognizer 当我使用UISwipeGestureRecognizer时也不会被调用
How can I detect when the user lifts his finger? 如何检测用户何时抬起手指?
I figured it out, actually it was quite easy, instead of using UISwipeGestureRecognizer to detect swipes I detected it myself using event handling 我想通了,实际上这很简单,而不是使用UISwipeGestureRecognizer来检测我自己使用事件处理检测到的滑动
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
self.initialPosition = [touch locationInView:self.view];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint movingPoint = [touch locationInView:self.view];
CGFloat moveAmt = movingPoint.y - self.initialPosition.y;
if (moveAmt < -(minimum_detect_distance)) {
[self handleSwipeUp];
} else if (moveAmt > minimum_detect_distance) {
[self handleSwipeDown];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self reset];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[self reset];
}
I haven't subclassed UIGestureRecognizer but did the event handling in the required view controller only, as in reset method I am resetting few variable, counters and timers belonging to the view controller. 我没有子类化UIGestureRecognizer但只在所需的视图控制器中进行事件处理,因为在重置方法中我重置了属于视图控制器的少量变量,计数器和计时器。
I think better you need to examine the state property of the gesture recognizer: 我认为你需要更好地检查手势识别器的状态属性:
- (void)swipe:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[recognizer view]];
if (recognizer.state == UIGestureRecognizerStateBegan)
NSLog(@"Swipe began");
else if (recognizer.state == UIGestureRecognizerStateEnded)
NSLog(@"Swipe ended");
}
UISwipeGestureRecognizer is a discrete gesture as the Apple documentation said, so you need to use a continuous gesture, in this case use UIPanGestureRecognizer. Apple文档说,UISwipeGestureRecognizer是一个离散的手势,所以你需要使用连续手势,在这种情况下使用UIPanGestureRecognizer。
Here is the code: 这是代码:
- (void)viewDidLoad{
[super viewDidLoad];
// add pan recognizer to the view when initialized
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
[panRecognizer setDelegate:self];
[yourView addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on
}
-(void)panRecognized:(UIPanGestureRecognizer *)sender{
CGPoint distance = [sender translationInView: yourView];
if (sender.state == UIGestureRecognizerStateEnded) {
[sender cancelsTouchesInView];
if (distance.x > 70 && distance.y > -50 && distance.y < 50) { // right
NSLog(@"user swiped right");
NSLog(@"distance.x - %f", distance.x);
} else if (distance.x < -70 && distance.y > -50 && distance.y < 50) { //left
NSLog(@"user swiped left");
NSLog(@"distance.x - %f", distance.x);
}
if (distance.y > 0) { // down
NSLog(@"user swiped down");
NSLog(@"distance.y - %f", distance.y);
} else if (distance.y < 0) { //up
NSLog(@"user swiped up");
NSLog(@"distance.y - %f", distance.y);
}
}
}
Don't forget to add UIGestureRecognizerDelegate. 不要忘记添加UIGestureRecognizerDelegate。
If you use scrollView, you can detect contentOffset of it 如果使用scrollView,则可以检测它的contentOffset
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y < -100 { // how you needed
// do what you need
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.