使用UIGestureRecognizer拖动UIView

14
我正在尝试构建一个GestureRecognizer,以从右向左拖动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];

}

我回答了你的问题,但是你应该提供更多细节,比如“我可以拖动视图,但不知道如何使其到达正确的位置”,... - rdurand
@rdurand 是的,我可以使用UIPanGestureRecognizer拖动视图,但我不知道如何准确地检测位置,然后将视图发送到所需位置。第二个问题是,如何仅水平拖动视图。 - darkman
提供一些代码。告诉我们你尝试了什么,哪些是不起作用的。 - rdurand
@rdurand 这是我正在尝试使用的代码。 - darkman
我添加了一些代码,你可以继续开发。 - rdurand
1个回答

33

请看这里。这是一个 UIGestureRecognizer 教程,它将为您提供处理捏合和平移手势的基础知识。一旦您学会了基础知识,就很容易实现您想要的功能。您只需要在完成平移后检查视图的位置,以将其发送到正确的位置。请查看这个关于UIScrollViews的其他教程,它可能会帮助您找到第二部分的方法。

这两个教程都来自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来进行某种“平滑”动画。它将有助于使您的动画更加“逼真”。在“if”开头添加以下代码:

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作为持续时间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接