动画UIImagevIew上的UITouch事件
我正在使用Snowfall示例代码,该代码从屏幕顶部删除UImages。我想检测UIImageview上的触摸并更新标签。如果我在IBOutlet中创建单个UIImageView并将其连接到触摸事件,则标签将正确更新。但是,当我尝试将其应用于图像视图时,它不起作用。以下是我到目前为止的情况:动画UIImagevIew上的UITouch事件,image,animation,random,uitouch,Image,Animation,Random,Uitouch,我正在使用Snowfall示例代码,该代码从屏幕顶部删除UImages。我想检测UIImageview上的触摸并更新标签。如果我在IBOutlet中创建单个UIImageView并将其连接到触摸事件,则标签将正确更新。但是,当我尝试将其应用于图像视图时,它不起作用。以下是我到目前为止的情况: - (void)viewDidLoad { [super viewDidLoad]; score = 0; self.view.backgroundColor = [
- (void)viewDidLoad {
[super viewDidLoad];
score = 0;
self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0];
flakeImage = [UIImage imageNamed:@"flake.png"];
[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
- (void)onTimer
{
flakeView = [[UIImageView alloc] initWithImage:flakeImage];
flakeView.opaque = YES;
flakeView.userInteractionEnabled = YES;
flakeView.multipleTouchEnabled = YES;
int startX = round(random() % 320);
int endX = round(random() % 320);
double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 100) + 1.0;
flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
[self.view addSubview:flakeView];
[self.view bringSubviewToFront:flakeView];
[UIView beginAnimations:nil context:flakeView];
[UIView setAnimationDuration:5 * speed];
flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale);
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == flakeView)
{
NSLog(@"tag %@",touch);
score = score + 1;
lbl1.text = [NSString stringWithFormat:@"%i", score];
}
}
动画通常禁用触摸处理,因此您必须使用:
animateWithDuration:delay:options:animations:completion
设置接收触摸的选项
IIRC您需要的选项是:UIViewAnimationOptionAllowUserInteraction在上述文档中查找完整列表中的UIViewAnimationOptions。[UIView AnimationWithDuration:0.1延迟:0.0选项:UIViewAnimationOptionAllowUserInteraction动画:^{[UIView beginAnimations:nil上下文:flakeView];[UIView setAnimationDuration:5*速度];flakeView.frame=CGRectMake(endX,500.0,25.0*比例,25.0*比例);[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:);[UIView setAnimationDelegate:self];[UIView commitAnimations];}完成:无];我以前尝试过此操作,但在将flakeView添加为子视图后将其放入。但仍然没有成功。动画块中的初始动画可能就是问题所在。要允许交互的每个动画都必须指定UIViewAnimationOptionAllowUserInteraction。以下是问题的解决方案。。