简体   繁体   中英

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"];

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.

I'm afraid the currently accepted answer is wrong:

When the user puts his finger on the screen, all the views are hit-tested with the touch position. However, when hit-testing them, the view's frame is requested. This is, however, linked to the modelLayer s frame, which is the end point of your animation.

What you would want is hit-testing the presentationLayer , but I guess this is not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM