我正在做这样的动画:
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; animation.duration = 100.0; animation.path = self.animationPath.CGPath; [view.layer addAnimation:animation forKey:@"animation"];
工作正常,但是,当尝试检测在屏幕上移动的对象上的触摸时,现在会失败:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { for (UIView* subview in self.subviews ) { if ( [subview hitTest:[self convertPoint:point toView:subview] withEvent:event] != nil ) { [self handleTap]; return YES; } } return NO; }
它失败了,因为视图的帧不再与它在动画时在屏幕上的明显位置相同.如何让pointInside与正在设置动画的视图一起使用?
解决方法
这是我执行此操作的代码
#import "ViewController.h" @interface ViewController () @property (weak,nonatomic) IBOutlet AnimatableView *animableView; @end @implementation ViewController - (void)animateAlongPath { UIBezierPath * path = [UIBezierPath bezierPathWithRect:self.view.frame]; CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; animation.duration = 10; animation.path = path.CGPath; animation.removedOnCompletion = NO; animation.fillMode = @"kCAFillModeForwards"; [self.animableView.layer addAnimation:animation forKey:@"animation"]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view,typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)animate:(id)sender { [self animateAlongPath]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView: [touch view]]; CALayer * selectedlayer = (CALayer*)[self.animableView.layer.presentationLayer hitTest:location]; if(selectedlayer != nil) NSLog(@"touched"); else NSLog(@"dont touched"); } @end
我希望这可以帮助你