为UIImageview设置Uianimation和Touch Event

我工作在一个项目,它需要animation从一个地方到另一个图像我完成,但我的问题是,当我animation我没有得到该UIImageview的任何触摸事件。任何知道请尽快解决。

- (void) imageSpawn { NSArray *images= [NSArray arrayWithObjects:[UIImage imageNamed:@"fish_right1.png"], [UIImage imageNamed:@"fish_right2.png"], [UIImage imageNamed:@"fish_right3.png"], [UIImage imageNamed:@"fish_right4.png"], [UIImage imageNamed:@"fish_right14.png"], [UIImage imageNamed:@"fish_right15.png"], [UIImage imageNamed:@"fish_right20.png"], nil]; int currentImageIndex=0; [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ [self.first_fish setImage:[images objectAtIndex:currentImageIndex] ]; }completion:Nil ]; UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ballTapped:)]; tapped.numberOfTapsRequired = 1; [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]; NSLog(@"LOCA X:%d",gesture.view.tag); NSLog(@"LOCA y:%f",location.y); } 

问候,Raja.I

有一个选项UIViewAnimationOptionAllowUserInteraction,您可以传递给animateWithDuration中的options参数:delay:options:animations:completion :. 您需要将其设置为允许在animation过程中进行交互。

编辑之后:这与你做animation的方式的性质有关。 如果您点击图像视图结束的地方(animation正在运行),您将看到手势识别器触发。 实际上,当animation开始时,视图的位置已经被设置为最终值。

为了使这个工作,我认为你必须做一个计时器而不是animateWithDuration。 创build一个重复计时器,并在每次调用时递增视图的x位置,并在到达目的地时使计时器无效。 这将允许您在移动时与视图进行交互。

当为视图设置animation时,您将视图设置为最终状态。 这样,您只能检测到animation的最终位置。

但是,可以绕过这个问题。 您需要捕捉触摸事件并与presentationLayer进行比较。

 -(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent *) event { CGPoint point = [[touches anyObject] locationInView:self.view]; if([self.cloudAnimate.layer.presentationLayer hitTest:point]) { //do something } } 

表示层具有关于与您的cloudAnimate关联的CALayer的视觉位置的信息。

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; if([imageView1.layer.presentationLayer hitTest:touchLocation]){ // TODO } }