Search code examples
iphoneioscocoa-touchipadquartz-2d

iPhone - Touch event is not received by UIImageView while animating using CABasicAnimation


i Have a UIImageview object moving along a path and i have use CABasic animation and BezierCure to move the object along the path, Now the problem is if i touch the moving object the touch event is not recognized by the UIImageView but the touch event is received by main view. can anybody help me to solve this problem? Below is my code

UITouch *touch = [[event allTouches] anyObject];
if (![touch.view isEqual: beaconPose1]) {
    return;
}
[touch.view.layer removeAllAnimations];

CGPoint location = [touch locationInView: self.view];

  CGRect frame = touch.view.frame;
frame.origin.x = location.x;
frame.origin.y  = location.y;
///frame.origin.x += xDisplacement;
//frame.origin.y += yDisplacement;
touch.view.frame = frame;

-- Code where User Interaction is enabled

[beaconPose1 setUserInteractionEnabled:true];

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 1.0f;
pathAnimation.path = beaconPose3Path.CGPath;
pathAnimation.calculationMode = kCAAnimationLinear;

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = pathAnimation.duration;
fullRotation.repeatCount = pathAnimation.duration/fullRotation.duration;
[beaconPose1.layer addAnimation:fullRotation forKey:@"360"];
[beaconPose1.layer addAnimation:pathAnimation forKey:@"movingAnimation"];

Solution

  • I figured out how to do this,

    The object under CAKeyframeAnimation or CABasicAnimation will not respond to touch event, instead what we can do is calculate if the location of the touch is withing the area of the object using Pythagorus theorem.