UIView 平移手势后消失

发布于 2024-10-09 01:08:26 字数 369 浏览 0 评论 0原文

我正在使用以下 IUPanGesture 处理程序。然而,当平移结束时,它正在移动的 UIView 就会消失。我还需要在这段代码中添加其他内容吗?

- (void)pan:(UIPanGestureRecognizer *)gesture
{
  if ((gesture.state == UIGestureRecognizerStateChanged) ||
  (gesture.state == UIGestureRecognizerStateEnded)) {

  CGPoint location = [gesture locationInView:[self superview]];

  [self setCenter:location];
  }
}

I am using the following handler for a IUPanGesture. However when the pan ends, the UIView that it is moving disappears. Do I need to add anything else to this code?

- (void)pan:(UIPanGestureRecognizer *)gesture
{
  if ((gesture.state == UIGestureRecognizerStateChanged) ||
  (gesture.state == UIGestureRecognizerStateEnded)) {

  CGPoint location = [gesture locationInView:[self superview]];

  [self setCenter:location];
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2024-10-16 01:08:26

我不知道它是否在任何地方都有记录,但我根据经验发现,除了 UIGestureRecognizerStateBeganUIGestureRecognizerStateChangedUIGestureRecognizerStateRecognized 之外的任何手势状态都会有垃圾触摸和位置信息。在许多情况下,代码甚至会在尝试访问不存在的触摸时崩溃。

因此,按如下方式更改条件应该可以解决您遇到的问题:

- (void)pan:(UIPanGestureRecognizer *)gesture
{
  if (gesture.state == UIGestureRecognizerStateChanged) {
    CGPoint location = [gesture locationInView:[self superview]];
    [self setCenter:location];
  }
}

I don't know if it's documented anywhere, but I've found empirically that any gesture states other than UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged, and UIGestureRecognizerStateRecognized will have garbage touch and location information. In many cases, the code would even crash trying to access a non-existent touch.

So, changing the condition as follows should fix the issue you're having:

- (void)pan:(UIPanGestureRecognizer *)gesture
{
  if (gesture.state == UIGestureRecognizerStateChanged) {
    CGPoint location = [gesture locationInView:[self superview]];
    [self setCenter:location];
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文