Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop and start an animation by touch. Objective C

I have made an animation that moves across the screen, my animation loops continuously. How can I stop the animation when you tap the animated image, Then let the animation continue when you lift the touch?

I know how to use TouchesMoved to move a specified button like this:

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

But getting it to work with my animation. I would like 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
like image 635
Blake Loizides Avatar asked Mar 28 '13 12:03

Blake Loizides


1 Answers

As you had already stated in your question ,you can get the touched point using

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

Then check check whether this point lies inside the coordinates of the animated UIImageview and then stop animation. But if you are using scrollview, you won't be able to use this because scrollview will not return any UIView touch events.

As your image view is animating, a better choice will be adding a UITapGestureRecogniser to the image view when you add it s subview, like this

- (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 the image view at the touched point, you can add this to ballTapped event:

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

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

like image 56
DD_ Avatar answered Oct 15 '22 01:10

DD_