0

これは単純なはずのようですが、そうではないようです。

私はストーリーボードを使用しており、最初のビューコントローラーはとして定義されていLogbookFirstViewControllerます。

このコントローラーの内容は、の中にありUIControlます。そうすれば、タップを検出できます。

ただし、ユーザーが画面をスワイプし始めた時期を簡単に判断する方法はわかりません。私がやりたいのは、x座標のタッチを取得することだけです。基本的にそれを追跡します。

UIPanGestureRecognizer私は内側を落とし、LogbookFirstViewControllerそれも出口に取り付けました:

.h

@property (assign) IBOutlet UIGestureRecognizer *gestureRecognizer;

もちろん、それを合成してデリゲートを設定しました。

.m

[gestureRecognizer setDelegate:self];

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touchLoc = [touches anyObject];
    CGPoint beginCenter = self.view.center;
    CGPoint touchPoint = [touchLoc locationInView:self.view];

    deltaX = touchPoint.x - beginCenter.x;
    deltaY = touchPoint.y - beginCenter.y;
    NSLog(@"X = %f & Y = %f", deltaX, deltaY);
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch * touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

    // Set the correct center when touched
    touchPoint.x -= deltaX;
    touchPoint.y -= deltaY;

    self.view.center = touchPoint;
}

ただし、これは何もしません。検出すらしません-(void)touchesBegan

私は何が欠けていますか?よろしくお願いします。

4

1 に答える 1

2

これらのメソッドはデリゲートメソッドではなく、サブクラス化専用ですUIGestureRecognizer

通常、ジェスチャレコグナイザーをインスタンス化し、そのジェスチャが認識されたときに呼び出すセレクター(メソッド)を指定してから、それをビューに割り当てます。次に例を示します。

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan]

次に、panメソッドでジェスチャから情報をクエリできます。

- (void)pan:(UIPanGestureRecognizer *)gesture
{
    // get information from the gesture object
}

StoryBoardでこれを行ったことはありませんが、View Controllerに既にプロパティがある場合は、ViewControllerメソッドで呼び出しaddTarget:action:てビューにアタッチできると思いますviewDidLoad

于 2013-01-23T02:42:30.080 に答える