停止UIPanGestureRecognizer在UIScrollView内部垂直移动UIView

时间:2013-04-10 02:13:05

标签: iphone ios objective-c cocoa-touch uigesturerecognizer

我想要一个只能水平移动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;
    }
}

谢谢!

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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

试试这个:创建一个新的单视图应用程序项目。在故事板中添加滚动视图。添加一个名为'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;
    }
}

就是这样。我不禁用滚动,但我们得到了我认为你正在寻找的行为。很高兴您的解决方案正在运行,但尝试这样的项目,看看它是否告诉您有关您的最新动态。