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:メソッドとメソッドでそれを行い-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 に答える