简体   繁体   English

一次由UILongPressGestureRecognizer触发拖动时,如何防止UIView“跳动”?

[英]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.

相关问题 加载 UIView 时如何执行我的方法? - How do I execute my method when the UIView loads? 将插入符号拖动到视图边缘时,如何使我的UITextView自动滚动? - How do I make my UITextView autoscroll when dragging caret to the edge of view? 使UIImageView对象在拖动时显示(使用UILongPressGestureRecognizer) - Getting a UIImageView object to show up when dragging (with UILongPressGestureRecognizer) 从iOS的边缘拖动时,如何模拟Android OS中的边缘发光动画? - How do I simulate the edge glowing animation like in Android OS when dragging from the edge in iOS? 如何禁用拖出我的可可应用程序的功能? - How do I disable dragging out of my Cocoa app? 如何从延迟加载get访问器中保留自定义UIView并将其分配给其他视图? - How do I retain my custom UIView from my lazy loading get accessor and assign it to other views? 要知道触发了哪个UILongPressGestureRecognizer - To know which UILongPressGestureRecognizer is triggered 拖放时如何清除文本字段? - How do I clear a textfield when dragging and dropping? UILongPressGestureRecognizer仅在移动手指时才能识别 - UILongPressGestureRecognizer recognize only when moving my finger 捕获拖动到UIControl之外— UILongPressGestureRecognizer - Capture dragging outside of UIControl — UILongPressGestureRecognizer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM