Stop and start the animation with a touch. Goal c

I made an animation that moves around the screen, my loop animation is continuous. How to stop the animation when you click on the animated image, and then continue the animation when you raise the touch?

I know how to use TouchesMoved to move the specified button as follows:

CGPoint point = [[[event allTouches] anyObject] locationInView:self.view]; UIControl *control = sender; control.center = point; 

But make it work with my animation. I want the animation to continue after I touch it.

SelectedCellViewController.h

 // SelectedCellViewController.h #import <Accounts/Accounts.h> #import <QuartzCore/QuartzCore.h> @interface SelectedCellViewController : UIViewController { IBOutlet UIImageView *imageView; UIImageView *rocket; } @end 

viewControllertoShow.m

 #import "SelectedCellViewController.h" @interface SelectedCellViewController () @end @implementation SelectedCellViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { } return self; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; [self performSelector:@selector(imageSpawn:) withObject:nil afterDelay:3]; } - (void) imageSpawn:(id) sender { UIImage* image = [UIImage imageNamed:@"ae"]; 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) { }]; [self.view addSubview:rocket]; UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ballTapped:)]; tapped.numberOfTapsRequired = 1; [rocket addGestureRecognizer:tapped]; [rocket setUserInteractionEnabled:YES]; } -(void)ballTapped:(UIGestureRecognizer *)gesture { CGPoint location = [gesture locationInView:gesture.view]; //then write code to remove the animation [self.view.layer removeAllAnimations]; NSLog(@"Tag = %d", gesture.view.tag); rocket.frame = CGRectMake(location.x,location.y,25,40); } - (void)dismissView { [self dismissViewControllerAnimated:YES completion:NULL]; } - (void)viewDidUnload { } @end 
+6
source share
3 answers

As you said in your question, you can get the affected point using

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

Then check if this point is inside the coordinates of the animated UIImageview , and then stop the animation. But if you use scrollview, you cannot use this because scrollview will not return any UIView touch UIView .

When your image is animated, the best choice would be to add a UITapGestureRecogniser to the image view when you add a preview of it, like

 - (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]; } 

And in the target function write code to stop the animation:

  -(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:

If you are trying to stop viewing the image at the affected point, you can add this to the ballTapped event:

 rocket.frame = CGRectMake(location.x,location.y,25,40); 

But for this you need to declare UIImageView *rocket outside this specific method, i.e. declare in your header file.

+3
source

Another way is to add this to the parent view of the animation -

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { self.animating = NO; return [super hitTest:point withEvent:event]; } 

use the (atomic) property, then check the property in the animation to see if it should be stopped. We use this to stop the photo gallery, which works so that the user can manually move the photos. You can also register here if the point is in a certain area, if necessary. This method is run before any sensory methods are called.

+1
source

try how it can be, it will help you, in the middle of the animation, if you touch the image, then the animation is removed from the view.

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint lastLocation = [touch locationInView: self.view]; if([[touch view] isKindOfClass:[UIImageView class]]){ [youImageView.layer removeAllAnimations]; youImageView.center=lastLocation;//assign your image view center when the animation removes from the view. } } 

and add the #import <QuartzCore/QuartzCore.h> .

0
source

All Articles