UIGestureRecognizer来拖动UIView [英] UIGestureRecognizer to Drag UIView

查看:88
本文介绍了UIGestureRecognizer来拖动UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建 GestureRecognizer 从右到左拖动 UIView 。如果用户将视图拖动到屏幕的一半,则视图将自动拖动到左上角以丢弃它,但如果用户不拖动到屏幕的一半,它将返回到屏幕右侧的角落。
这里很少丢失,任何教程或什么?
谢谢

I'm trying to build a GestureRecognizer to drag a UIView from right to left. If the user drag the View to the half of the screen, the View will be automatically dragged into the left corner to discard it, but if the user do not drag up to half the screen it will return to the corner right of the screen. Little lost here, any tutorials or something? Thanks

编辑:下面是一些代码,它是一个 UIImageView ,在 UIScrollView 中,我需要拖动整个卷轴或只是拖动里面的图像:

EDIT : Heres some code, its a UIImageView, inside a UIScrollView, i need to drag the entire scroll or just the image inside it :

_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];

}


推荐答案

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

Have a look over here. This is a UIGestureRecognizer tutorial which will give you the basics to handle pinch and pan gestures. Once you've learnt the basics, it's really easy to accomplish what you want. All you need is to check the position of the view once the pan is completed to send it to the correct position. Have a look at this other tutorial on UIScrollViews, it may help you find your way through the second part.

这两个教程都来自 raywenderlich.com ,可能是我所知道的最有用的iOS教程网站。

Both tutorials come from raywenderlich.com, probably the most useful iOS tutorials site I know.

以下是您可以处理的 handlePan:方法的一些代码:

Here is some code on your handlePan: method you can work on :

- (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];

}

我不会在这里给你完美答案,你我将不得不处理代码,找到一种方法让它按照您的意愿运行。

I won't give you the perfect answer here, you'll have to work on the code to find a way to make it work as you want it to.

这是最后一个提示。您可以使用slideFactor执行某种平滑动画。它将帮助您的动画更逼真。处理此代码,您可以在if的开头添加:

Here is one last tip. You can do some kind of "smooth" animation by using a slideFactor. It will help your animation being more "realistic". Work on this code, that you add right at the beginning of the "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 作为持续时间。

Then in UIView animateWithDuration:, use slideFactor as the duration.

这篇关于UIGestureRecognizer来拖动UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆