我制作了一个在屏幕上移动的动画,我的动画不断循环。如何在您点击动画图像时停止动画,然后在您解除触摸时让动画继续?
我知道如何使用 TouchesMoved 移动指定的按钮,如下所示:
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
但是让它与我的动画一起工作。我希望动画在我触摸它后继续。
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