UIView touchesbegan 在动画过程中没有响应 [英] UIView touchesbegan doesn't respond during animation

查看:26
本文介绍了UIView touchesbegan 在动画过程中没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承 UIImageView 的可拖动类.当视图没有动画时,拖动工作正常.但是在动画时它不会响应触摸.动画完成后,触摸再次起作用.但我需要它在触摸时暂停动画并在触摸结束时恢复.我花了一整天的时间研究它,但找不到原因.

I have a draggable class that inherits UIImageView. The drag works fine when the view is not animating. But when animating it won't respond to touches. Once the animation is completed the touch works again. But I need it to pause the animation on touches and resume when touch ends. I spent the whole day researching it but couldn't figure out the reason.

这是我的动画代码.

[UIView animateWithDuration:5.0f 
  delay:0 
  options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) 
  animations:^{ 
  self.center = CGPointMake(160,240);
  self.transform = CGAffineTransformIdentity;
  }
  completion:nil
];

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    NSLog(@"touch");
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
    [self.layer removeAllAnimations];
    [[self superview] bringSubviewToFront:self];
}

推荐答案

那是因为 ios 将您的动画视图放置在目标位置,当动画开始时,但将其绘制在路径上.因此,如果您在移动时轻按视图,实际上是轻按其框架之外的某处.

That's because ios places your animating view to the target position, when the animation starts, but draws it on the path. So if you tap the view while moving, you actually tap somewhere out of its frame.

在动画视图的初始化中,将 userInteractionEnabled 设置为 NO.所以触摸事件由superview处理.

In your animating view's init, set userInteractionEnabled to NO. So the touch events are handled by the superview.

self.userInteractionEnabled = NO;

在您的超级视图的 touchesBegan 方法中,检查您的动画视图的presentationLayer 位置.如果它们与触摸位置匹配,则将 touchesBegan 消息重定向到该视图.

In your superview's touchesBegan method, check your animating view's presentationLayer position. If they match with the touch position, redirect the touchesBegan message to that view.

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self.view];
    CGPoint presentationPosition = [[animatingView.layer presentationLayer] position];

    if (point.x > presentationPosition.x - 10 && point.x < presentationPosition.x + 10
        && point.y > presentationPosition.y - 10 && point.y < presentationPosition.y + 10) {
        [animatingView touchesBegan:touches withEvent:event];
    }
}

这篇关于UIView touchesbegan 在动画过程中没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆