6

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

我知道如何使用 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
4

3 回答 3

3

正如您在问题中已经说过的那样,您可以使用

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

然后检查该点是否在动画的坐标内UIImageview,然后停止动画。但是如果您使用的是滚动视图,您将无法使用它,因为滚动视图不会返回任何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];
}

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

    -(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在此特定方法之外声明,即在您的头文件中声明。

于 2013-03-28T12:50:04.383 回答
1

另一种方法是将其添加到动画的父视图中 -

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

使用(原子)属性,然后检查动画中的属性以查看是否应该停止。我们使用它来停止正在运行的照片库,以便用户可以手动移动照片。如有必要,如果该点位于特定区域,您也可以在此处签到。此方法在调用任何触摸方法之前运行。

于 2013-03-28T13:19:41.633 回答
0

尝试这样可能会对您有所帮助,如果您在动画中间触摸 imageview,那么动画将从视图中删除。

    - (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>框架。

于 2013-03-28T13:03:56.847 回答