我使用的是snow示例代码,它从屏幕顶部掉落UImages。我想在UIImageview上检测触摸并更新标签。如果我在IBOutlet中创建单个UIImageView并将其连接到触摸事件,标签就会正确更新。但当我尝试将它应用于掉落的UIImageView时,它不起作用。以下是目前为止的内容:
- (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
设置接收触摸的选项。
http://developer.apple.com/library/IOs/文档/WindowsViews/概念/ViewPG_iPhoneOS/AnimatingViews AnimatingViews.html #//apple_ref/doc/uid/TP40009503-CH6-SW1
IIRC你想要的选项是:UIViewAnimationOptionAllowUserInteraction寻找在上面的完整列表文档中的UIViewAnimationOptions