2

私が使用UISwipeGestureRecognizerし、上書きします

-(void)touchesBegan...,-(void)touchesEnded...,-(void)touchesMoved... methods.

touchesBeganとtouchsMovedは、スワイプジェスチャが認識され、touchesEndedが呼び出されなくなるまで(touchesCancelledと同じ)タッチを追跡し続けるようです。しかし、私は仕事をするためにスワイプジェスチャレコグナイザーとtouchesEndedの両方が必要です、どうすればそれを行うことができますか?

4

1 に答える 1

10

最初に、スワイプジェスチャ認識機能をライブラリからビューにドラッグアンドドロップします。

ss

そして、ビューでキャンセルされたアイテムをチェックします。

ss

スワイプジェスチャに応答するコードを記述します。

- (IBAction)swipe:(id)sender {
    v.backgroundColor = [UIColor blueColor];
}

次に、タッチデリゲートメソッドを記述します。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];    
    CGPoint pt = [touch locationInView:self];
    layer.frame = CGRectMake(pt.x, pt.y, 100, 100);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];    
    CGPoint pt = [touch locationInView:self];
    layer.frame = CGRectMake(pt.x, pt.y, 100, 100);
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    layer.frame = CGRectMake(0, 0, 100, 100);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.backgroundColor = [UIColor redColor];
}

これで、キャンセルせずに画像を移動でき、画面をスワイプして青色に設定できます(スワイプジェスチャは正常に認識されます)。両方できます。そして、タッチが終了すると、ウィンドウの色が赤に変わります。

ss

このサンプルプロジェクトをダウンロードして実行するだけです。

https://github.com/weed/p120812_TouchAndGesture

于 2012-08-12T10:50:13.150 に答える