单击一个动画图像/按钮 [英] Clicking an animating image / button

查看:221
本文介绍了单击一个动画图像/按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了高低对这样的解决方案,我已经无处得到。

I've looked high and low for a solutions for this and I've gotten nowhere.

我想开发一个iOS应用程序是pretty很多简单的水龙头,动画和播放声音。

I want to develop an iOS app that is pretty much a simple tap, animate and play sound.

所以我有地图的说,一个农场,当我点击羊,将动画,并提出一些噪音。我已成功地做到这一点和它都很好。

So I'd have a map of say a farm and when I click the sheep, it will animate and make some noise. I have managed to achieve this and its all good.

我的问题,问题是能够点击一个已经动画按钮。

My issue and question is being able to click an already animating button.

所以,当视图加载我想有云按钮从左至右在天空中移动向右慢慢地,我又已经达到了这个使用这个code

So when the view loads i would like to have cloud buttons move from left to right in the sky slowly, again I have already achieved this using this code

- (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应用程序的负载

BUT, when the buttons are animating from left to right and back they are not clickable so the touch I want for spinning the clouds and playing the sound doesn't work. I have read a few other posts that say that this can not be done but I have a load of apps on my iPhone and iPad that do this so anyone have any idea how this can be achieved?

任何帮助将大大AP preciated因为我拉我的头发了。

Any help would be greatly appreciated as I'm pulling my hair out.

先谢谢了。

编辑:

确定这样的感谢答案的组合下面我几乎得到了这个工作。

OK so thanks to a combination of answers below I have almost got this to work

所以我用这个来设定初始CGPoint:

So I am using this to set the initial CGPoint:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Move UIView

    cloudAnimate.center = CGPointMake(400.0, 100.0);
}

和从左到右我使用同样的code作为我本来有轻微TWEAK动画云:

And to animate the cloud from left to right I am using the same code as i originally had with a slight tweak:

- (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为我使用这样的:

And now instead of IBAction I am using this:

-(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(即使它会继续移动)应用程序崩溃。如果我打它,而它正朝着它发出声音和动画。

So now it does almost exactly what I want but if you hit the cloud when it is at its finishing CGPoint (even though it will carry on moving) the app crashes. If I hit it while it is moving it makes a sound and animate.

任何人有任何建议,这是为什么?

Anyones have any suggestions as to why this is?

推荐答案

在动画设置在其最终状态视图中的视图。这样,你可以只检测你在哪里动画的最终位置润色。

When you animate a view you set the view in its final state. This way you can only detect touches in the final position where you are animating.

然而,有可能去围绕这个问题。你需要捕捉触摸事件与比较的presentationLayer。

However, it is possible to go around that issue. You need to catch the touch event and comparate with the presentationLayer.

-(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent *) event {

    CGPoint point = [[touches anyObject] locationInView:self.view];

   if([self.cloudAnimate.layer.presentationLayer hitTest:point]) {

     //do something
   }
}

在presentation层具有对视觉与您cloudAnimate相关的CALayer的位置信息。

The presentation layer has information about the visual position of the CALayer that is associated with your cloudAnimate.

这篇关于单击一个动画图像/按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆