ios – UIGestureRecognizer拖动UIView

前端之家收集整理的这篇文章主要介绍了ios – UIGestureRecognizer拖动UIView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试构建一个GestureRecognizer来从右到左拖动一个UIView.如果用户将视图拖动到屏幕的一半,则视图将被自动拖动到左上角放弃,但如果用户不拖动到屏幕的一半,它将返回到屏幕右侧的角落.
有些迷失在这里,任何教程还是什么?
谢谢

编辑:在UIScrollView里面有一些代码,它的一个UIImageView,我需要拖动整个滚动条,或者拖动它的内部图像:

_myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(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,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];

}

解决方法

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

猜你在找的iOS相关文章