正如您在问题中已经指出的那样,您可以使用以下方法获取触摸点
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
然后检查该点是否位于动画的坐标内UIImageview
然后停止动画。
但是如果你使用scrollview,你将无法使用它,因为scrollview不会返回任何UIView
触摸事件。
当您的图像视图具有动画效果时,更好的选择是在添加子视图时将 UITapGestureRecogniser 添加到图像视图,如下所示
- (void) imageSpawn:(id) sender
{
UIImage* image = [UIImage imageNamed:@"ae"];
UIImageView *rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 200, 25, 40);
[UIView animateWithDuration:5
delay:0.2f
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^(){rocket.frame=CGRectMake(345, 200, 25, 40);}
completion:^(BOOL fin) {
}];
[myScrollView addSubview:rocket];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[self.rocket addGestureRecognizer:tapped];
[rocket setUserInteractionEnabled:YES];
}
并在目标函数中编写代码来停止动画:
-(void)ballTapped:(UIGestureRecognizer *)gesture
{
//here also you can get the tapped point if you need
CGPoint location = [gesture locationInView:gesture.view];
//then write code to remove the animation
[self.view.layer removeAllAnimations];
}
EDIT:
如果您尝试在触摸点停止图像视图,可以将其添加到 ballTapped 事件:
rocket.frame = CGRectMake(location.x,location.y,25,40);
但为此你必须声明UIImageView *rocket
在此特定方法之外,即在头文件中声明。