[英]To set Uianimation and Touch Event for UIImageview
我在一个项目中工作,需要将图像从一个地方动画化到另一个地方,我完成了,但是我的问题是在动画时我没有从该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:delay:options:animations:completion:中的options参数。 您需要设置它以允许在动画期间进行交互。
编辑后:这与制作动画的方式有关。 如果单击图像视图结束的位置(在动画运行时),您将看到手势识别器将触发。 实际上,动画开始时,视图的位置已经设置为最终值。
为了使此工作有效,我认为您必须使用计时器而不是animateWithDuration。 创建一个重复计时器,并在每次调用时增加视图的x位置,并在到达目的地时使计时器无效。 这将允许您在视图移动时与其进行交互。
为视图设置动画时,将视图设置为最终状态。 这样,您只能在动画制作的最终位置检测到触摸。
但是,有可能解决该问题。 您需要捕获触摸事件并将其与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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.