7

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

1 に答える 1

9

これは、アニメーションの開始時に ios がアニメーション ビューをターゲット位置に配置するが、パス上に描画するためです。したがって、移動中にビューをタップすると、実際にはそのフレームの外のどこかをタップすることになります。

アニメーション ビューの init で、userInteractionEnabled を NO に設定します。したがって、タッチ イベントはスーパービューによって処理されます。

self.userInteractionEnabled = NO;

スーパービューの 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];
    }
}
于 2010-12-13T19:09:57.190 に答える