9

私はiOSを初めて使用UIPanGestureRecognizerし、プロジェクトで使用しています。ビューをドラッグしているときに、現在のタッチポイントと以前のタッチポイントを取得する必要があります。私はこれらの2つのポイントを得るのに苦労しています。

メソッドを使用する場合、を使用touchesBeganする代わりにUIPanGestureRecognizer、次のコードでこれらの2つのポイントを取得できます。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    CGPoint previous=[[touches anyObject]previousLocationInView:self];
}

UIPanGestureRecognizerイベントファイア方式でこの2点を取得する必要があります。どうすればこれを達成できますか?案内してください。

4

5 に答える 5

17

これを使用できます:

CGPoint currentlocation = [recognizer locationInView:self.view];

見つからない場合は現在の場所を設定し、現在の場所を毎回追加して、以前の場所を保存します。

previousLocation = [recognizer locationInView:self.view]; 
于 2012-11-07T13:08:06.477 に答える
4

UIPanGestureRecognizerを IBAction にリンクすると、すべての変更でアクションが呼び出されます。ジェスチャ レコグナイザはstate、最初のかUIGestureRecognizerStateBegan、最後UIGestureRecognizerStateEndedか、または 間のイベントかを示す と呼ばれるプロパティも提供しますUIGestureRecognizerStateChanged

問題を解決するには、次のように試してください。

- (IBAction)panGestureMoveAround:(UIPanGestureRecognizer *)gesture {
    if ([gesture state] == UIGestureRecognizerStateBegan) {
        myVarToStoreTheBeganPosition = [gesture locationInView:self.view];
    } else if ([gesture state] == UIGestureRecognizerStateEnded) {
       CGPoint myNewPositionAtTheEnd = [gesture locationInView:self.view];
       // and now handle it ;)
    }
}

と呼ばれるメソッドも参照してくださいtranslationInView:

于 2012-11-07T13:08:43.853 に答える
2

何も保存したくない場合は、これを行うこともできます:

let location = panRecognizer.location(in: self)
let translation = panRecognizer.translation(in: self)
let previousLocation = CGPoint(x: location.x - translation.x, y: location.y - translation.y)
于 2019-03-15T16:08:12.250 に答える
0

次のように、パン ジェスチャ レコグナイザをインスタンス化する必要があります。

UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];

次に、ビューに panRecognizer を追加する必要があります。

[aView addGestureRecognizer:panRecognizer];

この- (void)handlePan:(UIPanGestureRecognizer *)recognizerメソッドは、ユーザーがビューを操作しているときに呼び出されます。handlePan: では、次のようにポイントに触れることができます。

CGPoint point = [recognizer locationInView:aView];

panRecognizer の状態を取得することもできます。

if (recognizer.state == UIGestureRecognizerStateBegan) {
    //do something
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
   //do something else
}
于 2012-11-07T13:10:06.450 に答える
0

UITouchには、ビュー内の前のタッチを取得する機能があります

  • (CGPoint)locationInView:(UIView *)view;
  • (CGPoint)previousLocationInView:(UIView *)view;
于 2014-12-26T09:12:57.680 に答える