通过触摸停止并开始动画.目标C.

Bla*_*des 6 cocoa-touch core-animation objective-c ios

我制作了一个在屏幕上移动的动画,我的动画不断循环.点击动画图像时如何停止动画,然后在触摸时让动画继续?

我知道如何使用TouchesMoved来移动指定的按钮,如下所示:

CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
Run Code Online (Sandbox Code Playgroud)

但让它与我的动画一起工作.我希望动画在我触摸后继续.

SelectedCellViewController.h

//  SelectedCellViewController.h

#import <Accounts/Accounts.h>
#import <QuartzCore/QuartzCore.h>

@interface SelectedCellViewController : UIViewController {
IBOutlet UIImageView *imageView;
UIImageView *rocket;
}

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

D_D*_*D_D 3

正如您在问题中已经指出的那样,您可以使用以下方法获取触摸点

CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
Run Code Online (Sandbox Code Playgroud)

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

当您的图像视图具有动画效果时,更好的选择是在添加子视图时将 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];
}
Run Code Online (Sandbox Code Playgroud)

并在目标函数中编写代码来停止动画:

    -(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]; 
     }
Run Code Online (Sandbox Code Playgroud)

编辑:

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

rocket.frame = CGRectMake(location.x,location.y,25,40);
Run Code Online (Sandbox Code Playgroud)

但为此,您必须UIImageView *rocket在该特定方法之外声明,即在头文件中声明。