UIGestureRecognizer拖动UIView

时间:2012-12-14 13:33:31

标签: ios uiview uigesturerecognizer

我正在尝试构建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];

}

1 个答案:

答案 0 :(得分:27)

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

这两个教程都来自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作为持续时间。