IOS 拖动、轻弹或轻弹 UIView [英] IOS drag, flick, or fling a UIView
问题描述
想知道我将如何轻弹或轻弹 UIView,例如 http://www.cardflick.co/ 或https://squareup.com/cardcase 演示视频.
Was wondering how I would go about flicking or flinging a UIView, such as http://www.cardflick.co/ or https://squareup.com/cardcase demo videos.
我知道如何拖动物品,但你如何赋予它们重力/惯性.这是IOS处理的吗?
I know how to drag items, but how do you give them gravity/inertia. Is this handled by IOS?
推荐答案
您所描述的模拟重力/惯性之王的那种效果可以通过缓出(开始快,结束慢)和缓入(开始慢,结束快)计时功能.
The kind of effects that you are describing as simulating a king of gravity/inertia can be produced by means of ease-out (start fast, end slow) and ease-in (start slow, end fast) timing functions.
iOS 中支持缓出和缓入,因此我认为您不需要任何外部库或努力工作(尽管您可以想象,您的效果需要大量微调).
Support for easing out and easing in is available in iOS, so I don't think you need any external library nor hard work (although, as you can imagine, your effect will need a lot of fine tuning).
这将动画对象平移到给定位置,并具有缓出效果:
This will animate the translation of an object to a given position with an ease-out effect:
[UIView animateWithDuration:2.0 delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^ {
self.image.center = finalPosition;
}
completion:NULL];
}
如果您通过 UIPanGestureRecognizer
处理您的手势,手势识别器将为您提供两个重要信息来计算最终位置:velocity
和 translation
,分别表示物体移动的速度和距离.
If you handle your gesture through a UIPanGestureRecognizer
, the gesture recognizer will provide you with two important information to calculate the final position: velocity
and translation
, which represent respectively how fast and how much the object was moved.
您可以在视图中安装平移手势识别器(我猜这将是您想要动画的对象),如下所示:
You can install a pan gesture recognizer in your view (this would be the object you would like to animate, I guess) like this:
UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[yourView addGestureRecognizer:panGestureRecognizer];
[panGestureRecognizer release];
然后在你的处理程序中处理动画:
And then handle the animation in your handler:
- (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>
}
}
这篇关于IOS 拖动、轻弹或轻弹 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!