Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 结合向下滑动和长按_Ios_Uigesturerecognizer - Fatal编程技术网

Ios 结合向下滑动和长按

Ios 结合向下滑动和长按,ios,uigesturerecognizer,Ios,Uigesturerecognizer,我试图在我的ViewController(它有一个tableView)中实现两个手势识别器,它们必须一个接一个地工作。第一个是向下滑动手势,第二个是长按手势 这是我用@sergio建议修改的代码 - (void)viewDidLoad { [super viewDidLoad]; swipeDown = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(s

我试图在我的ViewController(它有一个tableView)中实现两个手势识别器,它们必须一个接一个地工作。第一个是向下滑动手势,第二个是长按手势

这是我用@sergio建议修改的代码

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        swipeDown = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDownAction)] autorelease];

        longPress = [[[CustomLongPress alloc]initWithTarget:self action:@selector(longPressAction)] autorelease];


        longPress.minimumPressDuration = 2;

        swipeDown.numberOfTouchesRequired = 1;

        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;


       swipeDown.delegate = self ;

        longPress.delegate = self ;

        [myTableView addGestureRecognizer:swipeDown];

        [myTableView addGestureRecognizer:longPress];





  }


    -(void)swipeDownAction {

        _methodHasBeenCalled = YES;    // bool @property declared in .h

        NSLog(@"Swipe down detected");


    }



    -(void)longPressAction {

        NSLog(@"long press detected");
    }

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
以及我的UILongPressGestureRecognitor子类:

#import "CustomLongPress.h"
#import "ViewController.h"


@interface CustomLongPress()
{

    ViewController *vc;
}

@end


@implementation CustomLongPress
-(id)initWithTarget:(id)target action:(SEL)action controller:(ViewController *)viewCon
{
    self = [super initWithTarget:target action:action];

    if (self) {

        vc = viewCon;

    }

    return self;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     NSLog(vc.methodHasBeenCalled ? @"Yes" : @"No");

    if (vc.methodHasBeenCalled) {

        [super touchesBegan:touches withEvent:event];
    }
}

不幸的是,我仍然只能从swipeDown获得日志,但在长按时没有日志。如果我没有弄错的话,一旦系统检测到特定类型的手势(轻触、滑动、平移、长按等),它就不能再为该触摸动作发送不同类型的手势

这并不妨碍你达到你想要的结果。可能通过组合重载以下
UIResponder
方法:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

使用手势识别器(不确定输入是否会被一方使用而被另一方忽略),您可以确定这是一个“长刷”,您需要创建自己的自定义手势识别器。最好的方法是将
ui长按手势识别器子类化,使其仅在刷卡结束后“接受”长按。例如,在
触摸开始
方法中

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
   <if swipe recognizer action method has been called>
       [super touchesBegan:touches withEvent:event];
   }
}
其中
x>y
表示
x
需要
y
才能失败。因此,您的
C
(长按手势识别器)需要
B
GR失败,而
B
需要
A
(您的刷卡识别器)失败<只要
A
识别出刷卡,code>B
就会失败;一旦
B
失败,将允许
C
识别长按(如果有)

编辑:

阅读您的评论后,您是否介意尝试一下,看看它是否有帮助:

  • 删除覆盖的
    触摸开始
    并替换为:

    - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    }
    
  • 然后定义:

    - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    
      if (vc.methodHasBeenCalled) {
        [super touchesBegan:touches withEvent:event];
      } else {
         return;
      }
    }
    
  • 如果这不起作用,则使自定义手势识别器从通用手势识别器继承,然后

  • 触摸开始:
    留在原位,并将
    触摸移动后的
    替换为:

    - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    
      self.lastTouch = [touches anyObject];
      if (vc.methodHasBeenCalled && self.gestureNotBegunYet == YES) {
        self.gestureNotBegunYet = NO;
        [self performSelector:@selector(recognizeLongPress) withObject:nil afterDelay:1.0];
    
      } else {
         return;
      }
    }
    
  • 并加上:

    - (float)travelledDistance {
      CGPoint currentLocation = [self.lastTouch locationInView:self.view.superview];
      return sqrt(pow((currentLocation.x - self.initialLocation.x), 2.0) +
                pow((currentLocation.y - self.initialLocation.y), 2.0));
    }
    
    - (void)fail {
      self.gestureNotBegunYet = YES;
      [NSObject cancelPreviousPerformRequestsWithTarget:self];
    
    }
    
    - (void)recognizeLongPress {
    
      if ([self travelledDistance] < kTapDragThreshold) {
        self.longPressed = YES;
        self.state = UIGestureRecognizerStateChanged;
      } else {
            [self fail];
            self.state = UIGestureRecognizerStateFailed;
      }
    }
    
    - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
      [self fail];
      [super touchesEnded:touches withEvent:event];
    }
    
    - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
      [self fail];
      [super touchesCancelled:touches withEvent:event];
    }
    

    我要试试这个,非常感谢!另外,请检查我的编辑以获得另一种方法,如果您愿意,这种方法更为人为,但不需要子类化。
    @interface CustomLongPress()
    正确吗?你忽略了基类。。。您是否获得了触摸开始
    跟踪?是的,触摸开始的日志正在显示,并且
    @interface CustomLongPress
    正确。那么,当您进入触摸开始时,调用了什么
    vc.methods
    ?是否将其值添加到NSLog跟踪?
    - (float)travelledDistance {
      CGPoint currentLocation = [self.lastTouch locationInView:self.view.superview];
      return sqrt(pow((currentLocation.x - self.initialLocation.x), 2.0) +
                pow((currentLocation.y - self.initialLocation.y), 2.0));
    }
    
    - (void)fail {
      self.gestureNotBegunYet = YES;
      [NSObject cancelPreviousPerformRequestsWithTarget:self];
    
    }
    
    - (void)recognizeLongPress {
    
      if ([self travelledDistance] < kTapDragThreshold) {
        self.longPressed = YES;
        self.state = UIGestureRecognizerStateChanged;
      } else {
            [self fail];
            self.state = UIGestureRecognizerStateFailed;
      }
    }
    
    - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
      [self fail];
      [super touchesEnded:touches withEvent:event];
    }
    
    - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
      [self fail];
      [super touchesCancelled:touches withEvent:event];
    }
    
    @property(nonatomic) CGPoint initialLocation;
    @property(nonatomic, retain) UITouch* lastTouch;
    @property(nonatomic) BOOL gestureNotBegunYet;