IOS拖动,轻弹或者扔掉UIView

jdo*_*dog 12 drag-and-drop ios

想知道我将如何轻弹或推销UIView,例如http://www.cardflick.co/https://squareup.com/cardcase演示视频.

我知道如何拖动物品,但是你如何赋予它们重力/惯性.这是由IOS处理的吗?

ser*_*gio 42

你所描述的模拟重力/惯性之王的效果可以通过缓出(快速启动,慢速结束)和缓入(启动慢速,快速结束)定时功能来产生.

iOS中提供了对缓和和缓和的支持,因此我认为您不需要任何外部库也不需要努力工作(尽管如您所想,您的效果需要进行大量微调).

这将使对象平移到给定位置的动画具有缓出效果:

 [UIView animateWithDuration:2.0 delay:0
         options:UIViewAnimationOptionCurveEaseOut
         animations:^ {
               self.image.center = finalPosition;
         }
         completion:NULL];
 }
Run Code Online (Sandbox Code Playgroud)

如果您通过a处理手势UIPanGestureRecognizer,手势识别器将为您提供两个重要信息来计算最终位置:velocitytranslation,分别表示对象的移动速度和移动量.

你可以在你的视图中安装一个平移手势识别器(这就是你想要设置动画的对象,我想),如下所示:

    UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
    [yourView addGestureRecognizer:panGestureRecognizer];
            [panGestureRecognizer release];
Run Code Online (Sandbox Code Playgroud)

然后在处理程序中处理动画:

 - (void)handlePanFrom:(UIPanGestureRecognizer*)recognizer {

    CGPoint translation = [recognizer translationInView:recognizer.view];
    CGPoint velocity = [recognizer velocityInView:recognizer.view];

    if (recognizer.state == UIGestureRecognizerStateBegan) {    
    } else if (recognizer.state == UIGestureRecognizerStateChanged) {
        <track the movement>
    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
        <animate to final position>
    }
 }
Run Code Online (Sandbox Code Playgroud)

  • 什么是最终位置??? 不是最重要的部分!! (4认同)