如何进行滑动以杀死iOS 7应用程序切换器中使用的动画? [英] How do I make the swipe to kill animation that is used in the iOS 7 app switcher?

查看:148
本文介绍了如何进行滑动以杀死iOS 7应用程序切换器中使用的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对我的UIViewController视图顶部的自定义视图使用滑动动画。
用户应该能够将自定义视图向上滑动到顶部以将其发送出屏幕。完全像iOS7 App Switcher手势杀死一个应用程序:

I want to use a swipe animation for a custom view that is on top of the view of my UIViewController. The user should be able to swipe up the custom view to the top to send it out of the screen. Exactly like the iOS7 App Switcher gesture to kill an app:

目前我使用的是UIPanGestureRecognizer,它附加到我的自定义视图中。当在父视图的上100个像素中拖动自定义视图时,我启动UIAnimation以从父视图发送自定义视图:

At the moment I use a UIPanGestureRecognizer, that is attached to my custom view. When the custom view's is dragged in the upper 100 pixels of the parent view, I start an UIAnimation to send the custom view out of the parent view:

- (IBAction)panWasRecognized:(UIPanGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateEnded && _myView.center.y >= 100)
    {
        [self resetViewPosition];
    }
    else
    {
        CGPoint translation = [recognizer translationInView:_myView.superview];

        CGPoint center = _myView.center;
        center.x += translation.x;
        center.y += translation.y;
        _myView.center = center;

        if (center.y < 100)
        {
            NSLog(@"swipe up gesture detected ...");
            [self hideView];
        }

        [recognizer setTranslation:CGPointZero inView:_myView.superview];
    }
}

此方法有效,但效果不如iOS7应用程序切换器。有更好的方法吗?

This method is working, but not quite as well as the iOS7 app switcher. Is there a better way to do this?

编辑:

所以这对我有用:

- (IBAction)panWasRecognized:(UIPanGestureRecognizer *)recognizer
{
    CGPoint velocity = [recognizer velocityInView:recognizer.view];

    if (recognizer.state == UIGestureRecognizerStateEnded && _myView.center.y >= 150)
    {
        [self resetViewPosition];
    }
    else
    {
        CGPoint translation = [recognizer translationInView:_myView.superview];

        CGPoint center = _myView.center;
        center.x += translation.x;
        center.y += translation.y;
        _myView.center = center;

        if (recognizer.state == UIGestureRecognizerStateEnded && center.y < 150)
        {
            NSLog(@"swipe up gesture detected ...");

            // speed
            CGFloat xPoints = -170.0f;
            CGFloat velocityX = [recognizer velocityInView:self.view].x;
            NSTimeInterval duration = xPoints / velocityX;

            [self hideViewWithDuration:duration];
        }
    }

    [recognizer setTranslation:CGPointZero inView:_myView.superview];
}

- (void)hideViewWithDuration:(float)duration
{
    NSLog(@"Animation duration: %f", duration);

    [UIView animateWithDuration:duration
                          delay:0.0f
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^
     {
         CGRect frame = _myView.frame;
         frame.origin.y = -170;
         frame.origin.x = 10;
         _myView.frame = frame;
     }
                     completion:^(BOOL finished)
     {
         [_myView setHidden:YES];
         CGRect frame = _myView.frame;
         frame.origin.y = self.view.frame.size.height;
         frame.origin.x = 10;
         _myView.frame = frame;
         [_myView setHidden:NO];
         NSLog(@"View is hidden.");
         _hideInProgress = NO;
     }];
}

- (void)resetViewPosition
{       
    [UIView animateWithDuration:0.5
                          delay:0
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^
     {
         CGRect frame = _myView.frame;
         frame.origin.y = (self.view.frame.size.height - _myView.frame.size.height) / 2;
         frame.origin.x = 10;
         _myView.frame = frame;
     }
                     completion:^(BOOL finished)
     {
         NSLog(@"View is back to its original place.");
     }];
}


推荐答案

这里有几个部分。


  1. 发布点。它可以是在线,当视图被移除或在之下时,它将返回到起始位置。

  2. 释放期间的速度。如果速度很高,那么视图确实有足够的能量越过线。

所以,如果点是足够高或速度足够高,我们将删除视图,否则将返回视图。

So, if the point is "high enough" or the speed is "high enough", we will remove the view, or will return it back otherwise.

第三件事是删除/返回动画。它需要具有惯性才能表现得更加物理正确。你可以做一些简单的事情。让删除窗口动画的持续时间取决于1 /速度。你走得越快,动画就越快​​。

Third thing is removal/returning animation. It does need to have inertia to behave more physically correct. You can make simple thing. Let the duration to the "remove window" animation depend on 1/speed. So faster you go, faster the animation will be.

返回动画有点棘手。窗口必须稍微向上然后向下。这就是UIKit Dynamics可以派上用场的地方。

Return animation is a little bit trickier. Window has to go a little bit up and then down. It's where UIKit Dynamics can come handy.

我为我的项目做了除返回动画之外的所有事情,它看起来非常好。

I did all the things except "return animation" for my project and it looks very nice.

这篇关于如何进行滑动以杀死iOS 7应用程序切换器中使用的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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