Stop and start an animation by touch. Objective C

2020-06-17 08:05发布

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

3条回答
Lonely孤独者°
2楼-- · 2020-06-17 08:21

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 an (atomic) property, then check the property in the animation to see if it should be stopped. We use this to stop a photo gallery which is running so that the user can manually move the photos. You could also check in here if the point is in a specific area if necessary. This method runs before any touch methods are called.

查看更多
干净又极端
3楼-- · 2020-06-17 08:21

try like this may be it helps you, in the middle of the animation if you touch on the imageview then animation removes 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 #import <QuartzCore/QuartzCore.h> framework.

查看更多
We Are One
4楼-- · 2020-06-17 08:34

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.

查看更多
登录 后发表回答