我有一个侧滑菜单,使用UIScreenEdgePanGestureRecognizer来从屏幕边缘,由左至右滑动打开,由右至左滑动关闭。
现在的问题是,我从右至左快速滑动时(或者使用真机调试,手指从右向左,手机重新触碰屏幕边缘时),侧滑菜单就会停留在半打开的状态,这个时候检测不到if(sender.state == UIGestureRecognizerStateEnded)
.
我的代码如下:
self.panGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDo:)];
-(void)panGestureDo:(UIScreenEdgePanGestureRecognizer *)sender{
// NSLog(@"%ld",(long)sender.state);
if(sender.state == UIGestureRecognizerStateBegan){
self.startLocation = [sender locationInView:self.view];
}
self.currentLocation = [sender locationInView:self.view];
CGFloat distance = self.startLocation.x - self.currentLocation.x;
if(distance < 0){
[self.mainView addSubview:self.blurEffectView];
[self.blurEffectView addGestureRecognizer:self.tapGesture];
self.blurEffectView.alpha = (-distance)/100;
self.leftMenu.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.leftMenuWidth, self.view.frame.size.height);
self.mainView.frame =CGRectMake(self.view.frame.origin.x - distance, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
if (self.mainView.frame.origin.x > 97) {
self.mainView.frame =CGRectMake(self.view.frame.origin.x + self.leftMenuWidth, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self openMenuHalfAnimation];
}
if (self.mainView.frame.origin.x < 3) {
self.mainView.frame =CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self closeMenuHalfAnimation];
}
if(sender.state == UIGestureRecognizerStateEnded){
//当手势结束时,判断侧滑菜单的位置,自动打开或者关闭。
//但是当从右向左滑动速度过快,就不会进入这个if条件里面。
NSLog(@"UIGestureRecognizerStateEnded");
if (self.mainView.frame.origin.x > 50) {
[self openMenuHalfAnimation];
}else{
[self closeMenuHalfAnimation];
}
}
//下面的我都试过,检测不到滑动过快的情况。
if(sender.state == UIGestureRecognizerStateCancelled){
NSLog(@"UIGestureRecognizerStateCancelled");
}
if(sender.state == UIGestureRecognizerStateFailed){
NSLog(@"UIGestureRecognizerStateFailed");
}
if(sender.state == UIGestureRecognizerStatePossible){
NSLog(@"UIGestureRecognizerStatePossible");
}
// if(sender.state == UIGestureRecognizerStateChanged){
// NSLog(@"UIGestureRecognizerStateChanged");
// }
if(sender.state == UIGestureRecognizerStateBegan){
NSLog(@"UIGestureRecognizerStateBegan");
}
if(sender.state == UIGestureRecognizerStateRecognized){
NSLog(@"UIGestureRecognizerStateRecognized");
}
}
}
我还尝试过这些,但是都没有被调用。
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"touches end");
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"touches canceled");
}
원본 포스터로 문제가 해결되었나요?