14

を右から左GestureRecognizerにドラッグするように作成しようとしています。UIViewユーザーがビューを画面の半分にドラッグすると、ビューは自動的に左隅にドラッグされて破棄されますが、ユーザーが画面の半分までドラッグしないと、画面の右隅に戻ります。ここで少し失われました、チュートリアルか何か?ありがとう

編集:ここにいくつかのコードがあります、そのa UIImageView、aの中にUIScrollView、私はスクロール全体またはその中の画像だけをドラッグする必要があります:

_myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, self.window.bounds.size.height)]; [_myScroll setContentSize:CGSizeMake(self.window.bounds.size.width , 1300)];

_tutorial = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, 1300)]; [_tutorial setImage:[UIImage imageNamed:@"Default"]];
_tutorial.contentMode = UIViewContentModeScaleAspectFit; [_myScroll addSubview:_tutorial];

_tutorial.userInteractionEnabled = YES;
    UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:_tutorial action:@selector(handlePan:)]; 
 [_tutorial addGestureRecognizer:recognizer];

    [self.window addSubview:_myScroll];

そして、私がドラッグしようとする方法UIImageView

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:_myScroll];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:_myScroll];

}
4

1 に答える 1

33

こちらをご覧 ください. これは、UIGestureRecognizerピンチ ジェスチャとパン ジェスチャを処理するための基本を説明するチュートリアルです。基本を学べば、やりたいことが簡単に実現できます。パンが完了したら、ビューの位置を確認して正しい位置に送信するだけです。のこの他のチュートリアルをご覧くださいUIScrollViews。第 2 部の進め方を見つけるのに役立つかもしれません。

どちらのチュートリアルもraywenderlich.comから提供されています。これはおそらく、私が知っている中で最も役立つ iOS チュートリアル サイトです。


handlePan:あなたが取り組むことができるあなたの方法のいくつかのコードはここにあります:

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:_myScroll];

    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);

    if (recognizer.state == UIGestureRecognizerStateEnded) {

        // Check here for the position of the view when the user stops touching the screen

        // Set "CGFloat finalX" and "CGFloat finalY", depending on the last position of the touch

        // Use this to animate the position of your view to where you want
        [UIView animateWithDuration: aDuration
                              delay: 0 
                            options: UIViewAnimationOptionCurveEaseOut 
                         animations:^{
            CGPoint finalPoint = CGPointMake(finalX, finalY);
            recognizer.view.center = finalPoint; } 
                         completion:nil];
    }


    [recognizer setTranslation:CGPointMake(0, 0) inView:_myScroll];

}

ここで完璧な答えを出すことはできません。コードを調べて、希望どおりに機能させる方法を見つける必要があります。

これが最後のヒントです。slideFactor を使用すると、ある種の「滑らかな」アニメーションを実行できます。アニメーションをより「リアル」にするのに役立ちます。「if」の先頭に追加するこのコードに取り組みます。

CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;

float slideFactor = 0.1 * slideMult; // Increase for more slide

次に、 UIViewで、期間としてanimateWithDuration:使用します。slideFactor

于 2012-12-14T13:52:42.770 に答える