如何在UIPanGestureRecognizer中使用velocityView使拖动速度更快?
在理解如何为我下面使用的代码(由 @PaulSoltz 编写)实现更快的拖动速度时遇到一些困难,该代码允许您在屏幕上拖动对象。我意识到您必须使用 UIPanGestureRecognizer 中的velocityInView 方法,并且我知道它返回 x 速度向量和 y 速度向量。如果速度=随时间变化的距离,那么例如velocityx = (x2 - x1) / time
,我不确定如何使用这个公式来获得我需要的东西。基本上我只是想能够调整我的移动速度,使其更快一点。也许我想太多了,但如果有人能帮助我理解这一点,我将不胜感激。谢谢。
- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer {
UIView *myView = [gestureRecognizer view];
CGPoint translate = [gestureRecognizer translationInView:[myView superview]];
if ([gestureRecognizer state] == UIGestureRecognizerStateChanged || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[myView setCenter:CGPointMake(myView.center.x + translate.x, myView.center.y + translate.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[myView superview]];
}
}
Having a little trouble understanding how to implement a faster dragging speed for the code I'm using below (written by @PaulSoltz) which allows you to drag an object across the screen. I realize you have to use the velocityInView
method from UIPanGestureRecognizer
, and I understand it returns the x velocity vector and y velocity vector. If velocity = distance over time, then for instance velocityx = (x2 - x1) / time
and I'm unsure how to use this formula to get what I need. Basically I just want to be able to adjust the speed of my movement to make it a little faster. Maybe I'm over thinking things, but if anyone could help me understand this it would be appreciated. Thanks.
- (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer {
UIView *myView = [gestureRecognizer view];
CGPoint translate = [gestureRecognizer translationInView:[myView superview]];
if ([gestureRecognizer state] == UIGestureRecognizerStateChanged || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[myView setCenter:CGPointMake(myView.center.x + translate.x, myView.center.y + translate.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[myView superview]];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将平移向量的分量乘以某个常数即可。
Just multiply the components of the translation vector by some constant.