通过触摸停止并开始animation。 目标C

我制作了一个在屏幕上移动的animation,我的animation不断循环。 如何在点击animation图像时停止animation,然后在解除animation时继续播放animation?

我知道如何使用TouchesMoved移动一个指定的button,如下所示:

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

但让它与我的animation一起工作。 我想animation在我触摸后继续。

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 

正如你已经在你的问题中所述,你可以使用触摸点

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

然后检查此点是否位于animationUIImageview的坐标内,然后停止animation。 但是如果你使用的是scrollview,你将无法使用它,因为scrollview不会返回任何UIView触摸事件。

由于您的图像视图是animation,所以当您添加子视图时,更好的select是将UITapGestureRecogniser添加到图像视图中,像这样

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

并在目标函数中编写停止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]; } 

编辑:

如果您试图在触摸点停止图像视图,则可以将其添加到ballTapped事件:

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

但是为此,你必须在这个特定的方法之外声明UIImageView *rocket ,即在你的头文件中声明。

另一种方法是将此添加到animation的父视图 –

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

使用(primefaces)属性,然后检查animation中的属性,看看是否应该停止。 我们使用它来停止正在运行的照片库,以便用户可以手动移动照片。 如果需要的话,您也可以在这里办理入住手续。 此方法在任何触摸方法被调用之前运行。

尝试这样做可能会帮助你,在animation中间,如果你触摸的ImageView然后animation从视图中删除。

  - (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. } } 

并添加#import <QuartzCore/QuartzCore.h>框架。