2012-12-14 7 views
10

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

EDIT:はHERESにいくつかのコード、そのUIImageViewは、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]; 

} 
+0

私はあなたの質問に答えましたが、 "私はビューをドラッグすることができますが、正しい位置に行く方法を知らない"のように、あなたが試したことの詳細を与える必要があります... – rdurand

+0

@ rdurandはい、私はUIPanGestureRecognizerを使用してビューをドラッグできますが、私は正確にどのように位置を検出し、希望の位置にビューを送信するか分からない。そして2番目の問題は、ビューをドラッグする方法、水平方向のみです – darkman

+0

いくつかのコードを提供します。あなたが試したこと、*働かないことを教えてください。 – rdurand

答えて

22

hereをご覧ください。これはUIGestureRecognizerチュートリアルで、ピンチとパンのジェスチャーを処理するための基本を提供します。基本を学ぶと、あなたが望むものを達成するのは本当に簡単です。必要なのは、パンが完了したらビューの位置をチェックして、正しい位置に送信することだけです。 UIScrollViewsthis other tutorialを見てください。それは、あなたが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を使って、ある種の「滑らかな」アニメーションを行うことができます。あなたのアニメーションがよりリアルになるのを助けます。

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を使用する:あなたは、右の「if」の先頭に追加することを、このコードの作業、。

関連する問題