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
,手势识别器将为您提供两个重要信息来计算最终位置:速度
和翻译
,它们分别表示对象被移动的速度和速度。
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屋!