UITapGestures在动画中间

时间:2014-05-17 04:31:01

标签: ios uigesturerecognizer uiviewanimation

我正在尝试在使用UITapGestureRecognizers和UIViewAnimation在屏幕上移动时删除图像。我使用了UIViewAnimationOptionAllowUserInteraction,但我仍然收到SIGBART错误。我一直在寻找互联网,无法找到解决方案。

这是我的.m文件

#import "AbcViewController.h"


@interface AbcViewController ()

@end

@implementation AbcViewController

//single tap gesture action /deletes person
-(void) tapped:(UITapGestureRecognizer *)recognizer{
   [recognizer.view removeFromSuperview];
}

    //double tap gesture action
-(void) tapped2:(UITapGestureRecognizer *)recognizer{
[recognizer.view removeFromSuperview];
}

- (void)viewDidLoad
{



   [super viewDidLoad];

//this makes a zombie/person
UIImageView *zombieView =[[UIImageView alloc] initWithFrame:CGRectMake(135 , 300, 50, 75)];
UIImage *zombie=[UIImage imageNamed:@"free-vector-stick-figure-clip-art_105575_Stick_Figure_clip_art_hight.png"];
[zombieView setImage:zombie];
[self.view addSubview:zombieView];
[self.view insertSubview:zombieView belowSubview:_Railing];
[zombieView setUserInteractionEnabled:TRUE];

// double tap gesture recognizer
UITapGestureRecognizer *touch2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped2:)];
[touch2 setDelegate: self];
[touch2 setNumberOfTapsRequired: 2];
[zombieView addGestureRecognizer:touch2];

//tap gesture recognizer
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[touch setDelegate:self];
[touch setNumberOfTapsRequired: 1];
[zombieView addGestureRecognizer:touch];

//This makes the Person move down until he is behind the railing
[UIView animateWithDuration:8.5
                    delay:0.0
                    options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveLinear
                    animations:^{
                    CGAffineTransform transform= CGAffineTransformMakeTranslation(0,-200);
                    zombieView.transform = transform;
                   }
                 completion:nil];


};


-(void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end 

这是我的.h文件:

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

@interface AbcViewController : UIViewController <UIGestureRecognizerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *Railing;
//@property (strong, nonatomic) IBOutlet UIImageView *Person;
@property (strong, nonatomic) IBOutlet UIImageView *zombieView;
@property (strong, nonatomic) IBOutlet UIImage *zombie;

-(void) tapped:(UITapGestureRecognizer *)recognizer;
-(void) tapped2:(UITapGestureRecognizer *)recognizer;

@end

每次我在动画中期点击它都会出现错误,但是当它完成时它会正常工作。我对此很新,所以我确信它很简单,它始终是。但是,谢谢你的耐心和答案!

1 个答案:

答案 0 :(得分:0)

动画期间的对象不是您看到的对象。它已经移到了最终位置。动画是一种幻觉:“表示层”是动画的,但底层的“模型层”(真实对象)静止不动,并且在动画开始时已经移动。模型图层是视图中的图层,因此它是唯一可以点击的图层。

在动画制作动画时点击它是一个非常棘手的问题。我在这里讨论它:

http://www.apeth.com/iOSBook/ch18.html#_hit_testing_during_animation

或许这个讨论会给你一些想法。但无论如何,这都不容易。

(考虑一下,也许你不应该在这种游戏中使用视图动画。精灵套件是为这类东西而发明的。)