2011-09-20 17 views
11

私の意見にUIPanGestureRecognizerが追加されました。
問題は、タッチがパンジェスチャとして認識されるまでには、それは数点移動されなければならず、UIGestureRecognizerStateBegan状態で元のタッチ位置を抽出できないということです。 この状態でtranslationInView:は(0,0)であり、以降の移動はこのポイントから計算され、元のものからではありません。UIPanGestureRecognizerから元のタッチ位置を取得

ジェスチャ認識プログラム自体から元の場所を抽出する方法はありますか、またはtouchesBegan:メソッドを上書きする必要がありますか?

+0

このリンクは役立つかもしれない - http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/ – Coffee

答えて

7

あなたはジェスチャー認識器のためのdelegateを設定し、最初のタッチを取得するために

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 

を実装することができるはずです。

+2

これは、あまりにも、動作します。しかし、最後に私は 'touchesBegan:'を使いました。それは、パンジェスチャ認識機能は、実際にタッチイベントが始まった場所を知らないと思われます。 – antalkerekes

1

ではなくtranslationInView:を使用すると、相対座標の代わりに絶対座標が得られます。あなたが入力を取得するためにパンを開始しなければならないのが...これを回避するには、あなたのビューがUIButtonすることができ、あなたがそうのよう"touch down"に接続- (IBAction)buttonWasTouched:(UIButton *)button forEvent:(UIEvent *)eventをトリガすることができます

-(IBAction)shakeIntensityPanGesturebuttonWasTouched:(UIButton *)button forEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; //touch location local cordinates 
} 
3

あなたは、実装することによって、この問題を解決することができますPanGestureRecognizerオリジナルのタッチポイントを保存し、それを呼び出し元が利用できるようにするカスタム。

私はこの距離を移動して、移動距離を制御したいので、パンジェスチャーをトリガーしました。

これはあなたの状況には過剰であるかもしれませんが、完全に機能し、聞こえるほど難しいものではありません。

タッチポイントの座標を取得するには、あなただけ呼び出す:代わりの

[sender touchPointInView:yourView] 

#import "PanGestureRecognizer.h" 
#import <UIKit/UIGestureRecognizerSubclass.h> 

@implementation PanGestureRecognizer 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 
    UITouch *touch = [touches anyObject]; 

    // touchPoint is defined as: @property (assign,nonatomic) CGPoint touchPoint; 

    self.touchPoint = [touch locationInView:nil]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:nil]; 

    // customize the pan distance threshold 

    CGFloat dx = fabs(p.x-self.touchPoint.x); 
    CGFloat dy = fabs(p.y-self.touchPoint.y); 

    if (dx > 2 || dy > 2) { 
     if (self.state == UIGestureRecognizerStatePossible) { 
      [self setState:UIGestureRecognizerStateBegan]; 
     } 
     else { 
      [self setState:UIGestureRecognizerStateChanged]; 
     } 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesEnded:touches withEvent:event]; 

    if (self.state == UIGestureRecognizerStateChanged) { 
     [self setState:UIGestureRecognizerStateEnded]; 
    } 
    else { 
     [self setState:UIGestureRecognizerStateCancelled]; 
    } 
} 

- (void) reset 
{ 
} 

// this returns the original touch point 

- (CGPoint) touchPointInView:(UIView *)view 
{ 
    CGPoint p = [view convertPoint:self.touchPoint fromView:nil]; 

    return p; 
} 

@end 
+0

これをどのように使うべきか分かりません。 –

0

あなたはUIGestureRecognizerState

を使用することができます。

[sender locationInView:yourView] 

をここでPanGestureRecognizer.mためのコードです。

- (void) onPanGesture:(UIPanGestureRecognizer *)sender { 
    if(sender.state == UIGestureRecognizerStateBegan) { 
     origin = [sender locationInView]; 
    } else { 
     current = [sender locationInView]; 
     // Initial touch stored in origin 
    } 
} 

スウィフト(ISH):

var state = recognizer.state 
if state == UIGestureRecognizerState.Began { 
    println(recognizer.locationInView(viewForGesture)) 
    //this will be the point of first touch 
} 
+0

答えの説明を入力してください。 – Lal

+0

ジェスチャ認識プログラムの状態が異なります(.Began、.Ended、.Cancelledなど)。レコグナイザの状態が.Beganのときに、(表示またはタッチ)の位置を取得すると、最初の位置になります。 – Aiden

+0

ジェスチャによって呼び出される関数の内部にこのコードを配置します。上記のprintln行は、タッチ/パンごとに1つの座標ペアのみを出力します。 – Aiden

関連する問題