0

我想检测用户触摸释放,如果用户按住它,下面的代码

有效,但不要告诉我我是否正在握住(触摸并按住而不释放)触摸......

请帮我解决这个问题

[imageview setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(holdAction:)];
[singleTap setNumberOfTapsRequired:1];
[imageview addGestureRecognizer:singleTap];



- (void)holdAction:(UIGestureRecognizer *)holdRecognizer
{
    if (holdRecognizer.state == UIGestureRecognizerStateBegan) {


       NSLog(@"Holding Correctly. Release when ready.");

    } else if (holdRecognizer.state == UIGestureRecognizerStateEnded)
    {
       NSLog(@"You let go!");
    }
}
4

1 回答 1

1

-touchesBegan:withEvent:and-touchesEnded:withEvent:方法来做。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.isHolding = YES;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.isHolding = NO;
}

self.isHolding 是一个@property (nonatomic, assign) BOOL isHolding;
注意事项:在这些方法中,您可能需要执行额外的检查,如果触摸开始于特定视图以及它们已经结束的位置。
更新:相应地更改您的代码:

[imageview setUserInteractionEnabled:YES];
UILongPressGestureRecognizer *longPress =  [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(holdAction:)];
[imageview addGestureRecognizer:longPress];



- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
    if (holdRecognizer.state == UIGestureRecognizerStateBegan) {


       NSLog(@"Holding Correctly. Release when ready.");

    } else if (holdRecognizer.state == UIGestureRecognizerStateEnded)
    {
       NSLog(@"You let go!");
    }
}
于 2012-07-05T14:39:53.323 回答