【发布时间】:2013-12-25 03:58:54
【问题描述】:
我为此寻找了一个解决方案,但我无处可去。
我想开发一个 iOS 应用程序,它几乎是一个简单的点击、动画和播放声音。
所以我有一张农场地图,当我点击羊时,它会动画并发出一些声音。我已经设法实现了这一点,而且一切都很好。
我的问题和问题是能够点击一个已经动画的按钮。
因此,当视图加载时,我希望云按钮在天空中从左到右缓慢移动,我已经使用此代码实现了这一点
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidLoad];
[UIView animateWithDuration:8.5
delay:1.0
options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut
animations:^{
cloudAnimate.center = CGPointMake(-100.0, 100.0);
}
completion:NULL];
}
但是,当按钮从左到右再返回动画时,它们是不可点击的,因此我想要旋转云层和播放声音的触摸不起作用。我读过一些其他帖子说这不能完成,但我的 iPhone 和 iPad 上有很多应用程序可以做到这一点,所以有人知道如何实现这一点吗?
在我拔头发时,任何帮助都将不胜感激。
提前致谢。
编辑:
好的,多亏了下面的答案组合,我几乎可以做到这一点
所以我用它来设置初始CGPoint:
- (void)viewDidLoad
{
[super viewDidLoad];
// Move UIView
cloudAnimate.center = CGPointMake(400.0, 100.0);
}
为了从左到右为云设置动画,我使用的代码与最初的代码相同,但稍作调整:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidLoad];
[UIView animateWithDuration:8.5
delay:1.0
options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
cloudAnimate.center = CGPointMake(100.0, 100.0);
}
completion:NULL];
}
现在我使用的是 IBAction 而不是 IBAction:
-(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent *) event {
CGPoint point = [[touches anyObject] locationInView:self.view];
if([self.cloudAnimate.layer.presentationLayer hitTest:point]) {
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Super_Mario_Bros_Mushroom" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
cloudAnimate.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"panda-png-chewing"],[UIImage imageNamed:@"panda-png-happy"],nil];
cloudAnimate.imageView.animationDuration = 0.2;
cloudAnimate.imageView.animationRepeatCount = 6;
[cloudAnimate.imageView startAnimating];
}
}
所以现在它几乎完全符合我的要求,但是如果您在云达到其完成的 CGPoint(即使它会继续移动)时撞到云,应用程序就会崩溃。如果我在它移动时击中它,它会发出声音和动画。
有人对这是为什么有任何建议吗?
【问题讨论】:
-
试试 UIViewAnimationOptionAllowUserInteraction 选项
-
阅读我对这个问题的回答 (stackoverflow.com/questions/20367526/…),尤其是编辑后部分。