0

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

谢谢!

4

2 回答 2

2

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

[scrollView setScrollEnabled:NO];    // in case UIGestureRecognizerStateBegan
[scrollView setScrollEnabled:YES];   // in case UIGestureRecognizerStateEnded
于 2013-04-10T02:31:34.310 回答
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;
    }
}

而已。我没有禁用滚动,但我们得到了我认为您正在寻找的行为。很高兴您的解决方案正在运行,但请尝试这样的项目,看看它是否能告诉您有关您在做什么的任何信息。

于 2013-04-10T03:41:16.203 回答