UIView touchesbegan在动画期间没有响应

时间:2010-11-30 08:00:39

标签: objective-c uiview uiviewanimation

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

这是我的动画代码。

[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];
}

1 个答案:

答案 0 :(得分:9)

这是因为ios会在动画开始时将动画视图放置到目标位置,但会在路径上绘制它。因此,如果您在移动时点按视图,则实际上可以从框架中的某个位置点按。

在动画视图的init中,将userInteractionEnabled设置为NO。因此,触摸事件由superview处理。

self.userInteractionEnabled = NO;

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

- (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];
    }
}