[英]How do I keep my UIView from “jumping” when dragging it once triggered by UILongPressGestureRecognizer?
Intended function: 预期功能:
After long-pressing a button, have the ability to drag it around on the screen. 长按一个按钮后,可以在屏幕上拖动它。
Jumping Bug: 跳虫:
When I initially start dragging the button, its center "jumps" to the CGPoint that registered my initial click to trigger the long press. 最初开始拖动按钮时,其中心会“跳转”到CGPoint,该CGPoint记录了我的初始单击以触发长按。 Eg, I long-press click a button on its top right, and once I start dragging the cursor while holding it the button jumps to that "top right" location.
例如,我长按一下其右上角的按钮,并在按住它开始拖动光标后,该按钮将跳至该“右上”位置。
After that jump - all dragging is fine. 跳转之后-所有拖动都可以了。
Code: 码:
- (void)longPress:(UILongPressGestureRecognizer*)receivedGesture
{
if (receivedGesture.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [receivedGesture locationInView:self.scrollView];
pannedBadge.center = translation;
}
}
Ultimate question: 终极问题:
What's the solution here? 这里有什么解决方案? How do I make it such that the initial dragging moves the button from its original center?
如何使初始拖动将按钮从其原始中心移出?
Thanks! 谢谢!
通常,要使视图可拖动,我要做的是检测触摸的起点,然后获取其移动(触摸)的差异,然后按如下所示设置视图的中心:我没有将其设置为绝对触摸,但我使用刚刚检测到的触摸移动来相对设置它。
The reason is that your translation point is asking the gesture's location in the view, not the center of the view it's over. 原因是您的翻译点是在询问手势在视图中的位置,而不是其上方的视图中心。 If you start pannedBadge.center to be the button's center, I that should work.
如果您将pannedBadge.center启动为按钮的中心,那应该可以。 Hope this helps!
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.