我使用以下代码构建一个按钮的简单缩放效果来构建一个简单的游戏。 我希望用户触摸到导致缩放期内某些内容的按钮。如果他们在那段时间内无法触及它,那么其他一些事情就会发生。
但是使用下面的代码,该按钮只能在动画完成后检测到触摸。我真的不想要。我希望它能够在动画期间被检测到。为了做到这一点,我应该使用什么代码?
谢谢
-(void) start{
hit1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[hit1 addTarget:self action:@selector(rolebutton:) forControlEvents:UIControlEventTouchUpInside];
[hit1 setFrame:CGRectMake(575, 255, 45, 45)];
hit1.translatesAutoresizingMaskIntoConstraints = YES;
[hit1 setBackgroundImage:[UIImage imageNamed:@"roles.png"] forState:UIControlStateNormal];
[hit1 setExclusiveTouch:YES];
hit1.transform = CGAffineTransformMakeScale(0.01, 0.01);
[self.view addSubview:hit1];
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^{
hit1.transform = CGAffineTransformMakeScale(1, 1);
hit1.alpha = 1;
}
completion:^(BOOL finished){
if (finished)
{
[hit1 removeFromSuperview];
NSLog(@"customView Displayed .....");
}
}];
}
-(void) rolebutton:(UIButton*) sender{
NSLog(@"hit");
}/*
答案 0 :(得分:2)
使用动画选项UIViewAnimationOptionAllowUserInteraction
。
-(void) start{
hit1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[hit1 addTarget:self action:@selector(rolebutton:) forControlEvents:UIControlEventTouchUpInside];
[hit1 setFrame:CGRectMake(575, 255, 45, 45)];
hit1.translatesAutoresizingMaskIntoConstraints = YES;
[hit1 setBackgroundImage:[UIImage imageNamed:@"roles.png"] forState:UIControlStateNormal];
[hit1 setExclusiveTouch:YES];
hit1.transform = CGAffineTransformMakeScale(0.01, 0.01);
[self.view addSubview:hit1];
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
hit1.transform = CGAffineTransformMakeScale(1, 1);
hit1.alpha = 1;
}
completion:^(BOOL finished){
if (finished)
{
[hit1 removeFromSuperview];
NSLog(@"customView Displayed .....");
}
}];