简体   繁体   中英

To set Uianimation and Touch Event for 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);

        }

Regards, Raja.I

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 Edit: 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.

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.

When you animate a view you set the view in its final state. This way you can only detect touches in the final position where you are animating.

However, it is possible to go around that issue. You need to catch the touch event and compare with the presentationLayer.

-(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent *) event {

    CGPoint point = [[touches anyObject] locationInView:self.view];

    if([self.cloudAnimate.layer.presentationLayer hitTest:point]) {
        //do something
    }
}

The presentation layer has information about the visual position of the CALayer that is associated with your cloudAnimate.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if([imageView1.layer.presentationLayer hitTest:touchLocation]){
       // TODO
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM