为UIImageview设置Uianimation和Touch事

为UIImageview设置Uianimation和Touch事

本文介绍了为UIImageview设置Uianimation和Touch事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中工作,需要将图像从一个地方动画化到另一个地方,我完成了,但是我的问题是在动画时我没有从该UIImageview中获得触摸事件.所以任何知道的都请提供解决方案尽快.

I'm Working in a project it need to animate the image from one place to another i complete that but my problem is while i animating i don't get the touch event from that UIImageview .so any know please give the solution asap.

- (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);

        }

关于,拉贾·我

推荐答案

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

There's an option, UIViewAnimationOptionAllowUserInteraction, that you can pass to the options parameter in animateWithDuration:delay:options:animations:completion:. You need to set that to allow interactions during an animation.

编辑后:这与您制作动画的方式的本质有关.如果单击图像视图结束的位置(在动画运行时),您将看到手势识别器将触发.实际上,动画开始时,视图的位置已经设​​置为最终值.

After This has to do with the nature of the way you're doing the animation. If you click on the place where your image view ends up (while the animation is running) you will see that the gesture recognizer fires. In effect, the location of the view is already set to the final value when the animation starts.

要使其工作,我认为您必须使用计时器而不是animateWithDuration.创建一个重复计时器,并在每次调用时增加视图的x位置,并在到达目的地时使计时器无效.这样,您就可以在视图移动时与它进行交互.

To make this work, I think you have to do it with a timer instead of animateWithDuration. Create a repeating timer, and increment the x position of your view with each call, and invalidate the timer when you reach the destination. This will allow you to interact with the view while it moves.

这篇关于为UIImageview设置Uianimation和Touch事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 11:44