2013-04-10 38 views
0

我想要一个只能水平移动UIView的平移手势。停止UIPanGestureRecognizer在UIScrollView中垂直移动UIView

到目前为止,在移动手势的开始,只有水平移动是允许的,但一旦 平移手势已经开始,UIView的水平和垂直移动

  • 我如何停止UIView随时垂直移动
  • 有没有什么办法可以让UIView的顶端始终在屏幕的顶端

即它从来没有从它的设定位置

以下垂直移动是我当前的代码:

- (void)panePanned:(UIPanGestureRecognizer *)gestureRecognizer 
{  
    switch (gestureRecognizer.state) { 
     case UIGestureRecognizerStateBegan: { 
      self.paneStartLocation = [gestureRecognizer locationInView:self.mainView]; 
      self.paneVelocity = 0.0; 
      break; 
     } 
     case UIGestureRecognizerStateChanged: { 
      CGPoint panLocationInPaneView = [gestureRecognizer locationInView:self.mainView]; 
      CGFloat velocity = -(self.paneStartLocation.x - panLocationInPaneView.x); 
      CGRect newFrame = self.mainView.frame; 

      newFrame.origin.x += (panLocationInPaneView.x - self.paneStartLocation.x); 
      if (newFrame.origin.x < 0.0) newFrame.origin.x = 0.0; 
      self.mainView.frame = newFrame; 

      if (velocity != 0) { 
       self.paneVelocity = velocity; 
      } 
      break; 
     } 
     case UIGestureRecognizerStateEnded: { 
      [self animate]; 
      break; 
     } 
     default: 
      break; 
    } 
} 

谢谢!

+0

我只看到mainView框架的origin.x更新。此代码中没有任何内容表示y值正在改变。也许NSLog(@“%f”,self.mainView.frame.origin.y);开状态改变了?让我们看看它是否在变化。 – danh 2013-04-10 02:18:40

+0

我检查了它,它始终为0表示origin.y :(@danh – GangstaGraham 2013-04-10 02:19:48

+0

另外,我也不更改动画方法中的y值,所以我对发生的事情感到困惑。@danh – GangstaGraham 2013-04-10 02:21:04

回答

2

尝试禁用开始状态的滚动并在状态结束时重新启用状态。

[scrollView setScrollEnabled:NO]; // in case UIGestureRecognizerStateBegan 
[scrollView setScrollEnabled:YES]; // in case UIGestureRecognizerStateEnded 
+0

好主意,但它似乎会导致干扰平移手势和滚动视图有自己的平移手势,我将与这个想法一起工作,看看我是否可以通过@danh – GangstaGraham 2013-04-10 02:36:28

+0

我会看看我是否可以编写一个soln。 – danh 2013-04-10 02:38:24

+0

这工作得很好,用if(velocity> 0。0)[scrollView setScrollEnabled:NO];'在UIGestureRecognizerStateChanged和'[scrollView setScrollEnabled:YES];'在UIGestureRecognizerStateEnded @danh – GangstaGraham 2013-04-10 02:48:21

1

我确实通过自己构建了一些东西来学习一些东西。我认为泛逻辑可以简化,我认为可以在不禁用滚动的情况下工作。

试试这个:创建一个新的单视图应用程序项目。在故事板中添加滚动视图。添加一个名为'scrollView'的连接到ViewController的插座。在ViewController.m添加以下代码:

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView; // connected in storyboard 
@end 

@implementation ViewController 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    for (int i=0; i<60; i++) { 
     UIView *draggableView = [[UIView alloc] initWithFrame:CGRectMake(10, i*40, 34, 34)]; 
     draggableView.backgroundColor = [UIColor redColor]; 

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

     [self.scrollView addSubview:draggableView]; 
    } 
    self.scrollView.contentSize = CGSizeMake(320, 60*40); 
} 

这增加了大量的滚动视图可拖动的观点,给每个全景手势识别。我做了一个像你的平底锅的方法,除了更简单...

- (void)pan:(UIPanGestureRecognizer *)gr { 

    switch (gr.state) { 
     case UIGestureRecognizerStateBegan: { 
      break; 
     } 
     case UIGestureRecognizerStateChanged: { 
      UIView *view = gr.view; 
      CGRect frame = view.frame; 

      gr.view.frame = CGRectMake([gr translationInView:view].x, frame.origin.y, frame.size.width, frame.size.height); 
      break; 
     } 
     case UIGestureRecognizerStateEnded: { 
      break; 
     } 
     default: 
      break; 
    } 
} 

就是这样。我不禁止滚动,但我们得到了我认为你在寻找的行为。很高兴你的解决方案正在工作,但尝试一个这样的项目,看看它是否告诉你任何关于你想要的东西。

+0

这绝对是一个更简单的解决方案,但是,它不会阻止UIView始终垂直移动。再次感谢您的帮助。 :) – GangstaGraham 2013-04-11 03:49:38