2013-04-10 35 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