2012-12-14 67 views
10

我试图建立一个GestureRecognizer拖动一个UIView从右到左。如果用户将视图拖动到屏幕的一半,视图将自动拖动到左角以放弃它,但如果用户不拖动到屏幕的一半,它将返回到屏幕右侧的角落。 这里很少迷失,任何教程或什么? 感谢UIGestureRecognizer拖动UIView

编辑:继承人一些代码,它是一个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拖动视图,但我不知道如何检测位置,然后将视图发送到所需的位置。第二个问题是,如何拖动视图,只能水平地提供 – darkman

+0

提供一些代码。告诉我们你试过了什么,什么*不行*。 – rdurand

回答

22

看看here。这是一个UIGestureRecognizer教程,它将为您提供处理捏和平移手势的基础知识。一旦你了解了基础知识,就可以很容易地完成你想要的任务。所有你需要的是一旦平底锅完成后检查视图的位置,将其发送到正确的位置。看看UIScrollViews上的this other tutorial,它可以帮助你找到通过第二部分的方法。

这两个教程都来自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的持续时间。