在相同视图中添加两个短长手势并进行适当区分

时间:2016-09-07 08:22:58

标签: ios objective-c touch uigesturerecognizer

我正在开发应用程序,其中我想在相同的视图中进行短暂的长手势,我添加但问题是我面对的是短手势结束始终打电话,需要你的帮助如何以正确的方式做到这一点。以下是我的代码。

        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGestureOnFormFields:)];
    longGesture.minimumPressDuration = 1.0f;
    [longGesture setDelegate:self];
    [self addGestureRecognizer:longGesture];

    UILongPressGestureRecognizer *shortGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(shortGesture:)];
    shortGesture.minimumPressDuration = 0.1f;
    [shortGesture setDelegate:self];
    [self addGestureRecognizer:shortGesture];

- (void)longGestureOnFormFields:(UIGestureRecognizer*) recogniser
{
    if (recogniser.state == UIGestureRecognizerStateEnded ) {
}
- (void)shortGesture:(UIGestureRecognizer*) recogniser
    {
        if (recogniser.state == UIGestureRecognizerStateEnded ) {
}

1 个答案:

答案 0 :(得分:0)

您可以使用UITapGestureRecognizer之类的

来管理它
 self.view.userInteractionEnabled = YES;

UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGestureOnFormFields:)];
longGesture.minimumPressDuration = 1.0f;
[longGesture setDelegate:self];
[self.view addGestureRecognizer:longGesture];


UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(testm)];
[self.view addGestureRecognizer:recog];

你的方法就像是,

-(void)testm{


NSLog(@"tap");

}

-(void)longGestureOnFormFields : (UILongPressGestureRecognizer*)sender{

   if (sender.state == UIGestureRecognizerStateEnded ) {

       NSLog(@"long press gesture");
   }

}

我已经在viewDidload中添加了手势识别器,即在viewcontroller中,所以如果您有子类self.view并将其添加到该视图,则将其添加到UIVIew,然后将其添加到{{1}像self一样,如图所示!