我正在尝试为 UIButton 制作动画以在屏幕上移动。用户可以随时触摸它。然而,它似乎在移动时不响应触摸,仅在动画的开始和结束时响应。我想这是因为按钮本身没有移动,只是它的图像在移动。我有什么想法可以解决这个问题吗?到目前为止,这是我的代码。谢谢!
- (void)viewDidLoad {
[self newBubble];
[super viewDidLoad];
}
- (void)newBubble {
UIButton *bubble = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
bubble.frame = CGRectMake(10, 380, 50, 50);
UIImage *bubbleImage = [UIImage imageNamed:@"bubble.png"];
[bubble setBackgroundImage:bubbleImage forState:UIControlStateNormal];
bubble.imageView.contentMode = UIViewContentModeScaleAspectFit;
[bubble addTarget:self action:@selector(bubbleBurst:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:bubble];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0];
CGAffineTransform newTransform = CGAffineTransformMakeScale(1.5, 1.5);
bubble.transform = CGAffineTransformTranslate(newTransform, 10, -200);
[UIView commitAnimations];
}
- (IBAction)bubbleBurst:(id)sender {
NSLog(@"Bubble burst");
UIButton *bubbleBurst = sender;
[bubbleBurst removeFromSuperview];
}
The +[UIView animateWithDuration:delay:options:animations:completion:]
方法需要一个UIViewAnimationOptions
争论。如果你包括UIViewAnimationOptionAllowUserInteraction
,您应该能够在动画播放时按下按钮:
[UIView animateWithDuration:5.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
CGAffineTransform newTransform = CGAffineTransformMakeScale(1.5, 1.5);
bubble.transform = CGAffineTransformTranslate(newTransform, 10, -200);
} completion:nil];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)