2016-05-01 97 views
1

我从来没有做过之前的任何编码,直到几个星期前进行的行动,并同时我对我自己做出合理的进展,我与挣扎的东西,我希望有人不会介意帮助。如何在简单的UIView动画

我试图做一个简单的应用程序为我女儿斯威夫特(的Xcode 7.3版 - 7D175)。这是一系列动画,角色在月球陨石坑内上下跳动。

我希望能够做的,是调用一个函数时按下动画UIImages之一。理想情况下,我想它来播放声音按下动画的时候(我知道该怎么称呼的声音,只是没有以这种方式)

我创建了一个类,我写的代码为动画

func alienAnimate() { 

    UIView.animateWithDuration(0.3, delay: 0, options: [.CurveLinear, .AllowUserInteraction], animations: { 
     self.center.y -= 135 
      }, completion: nil) 

    UIView.animateWithDuration(0.3, delay: 3, options: [.CurveLinear, .AllowUserInteraction], animations: { 
     self.center.y += 135 
      }, completion: nil) 

    } 

如果有人能在正确的方向指向我,我会非常感谢。我猜可能有更好的动画方式,但这是我现在知道的。

编辑 - 5月2日

行,所以我最终得到了它的工作...好,几乎:)它仍然无法正常工作正是我想,但我的工作就可以了。

的动画基本上沿(所以外星人弹出像他们出来一个洞)的路径垂直移动。下面的代码可以工作,但仍然允许您在它的路径开始处点击图像,当它实际上位于一个洞的下方时。

我还需要解决如何单击它实际上是现在,而不能按它的出发点。

View of App

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch = touches.first 
    let touchLocation = touch!.locationInView(self.view) 
    if self.imgAlien.layer.presentationLayer()!.hitTest(touchLocation) != nil { 
     sfxPop.play() 

    } else if self.imgAlien.layer.presentationLayer()!.hitTest(touchLocation) != nil { 
     sfxPop.play() 
    } else if self.imgAlien2.layer.presentationLayer()!.hitTest(touchLocation) != nil { 
     sfxPop.play() 
    } else if self.imgAlien3.layer.presentationLayer()!.hitTest(touchLocation) != nil { 
     sfxPop.play() 
    } else if self.imgAlien4.layer.presentationLayer()!.hitTest(touchLocation) != nil { 
     sfxPop.play() 
    } else if self.imgAlien5.layer.presentationLayer()!.hitTest(touchLocation) != nil { 
     sfxPop.play() 
    } else if self.imgUFO.layer.presentationLayer()!.hitTest(touchLocation) != nil { 
     sfxPop.play() 

} 

}

+0

道歉,如果这是没有足够的信息,开心张贴更多,如果需要,我只是不想太多的信息,如果它不需要:) –

+0

我没有看到任何代码相关的图像在这里..只是移动的子类。如果它确实是外星人类,让我们知道,以便我们可以回答它 – Shubhank

+1

这听起来像你想要做的事情出来的盒子与Sprite套件。你可能想看看一个Sprite套件教程,看看它是否值得改变你的实现 – bolnad

回答

1

假设你有你的星球和动画往下运作起来,如你所愿,你会怎么做是为了检测触摸:

这只是一个例子:

var image : UIImage = UIImage(named:"alien.png")! 
var image2 : UIImage = UIImage(named:"alienBoss.png")! 
var alien1 = UIImageView(image: image) 
var alien2 = UIImageView(image: image) 
var alienBoss = UIImageView(image: image2) 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    let touch = touches.first as! UITouch 
    if(touch.view == alien1){ 
     // User touch alien1, do whatever you want 
    } else 
    if(touch.view == alien2){ 
     // User touch alien2, do whatever you want 
    } else 
    if(touch.view == alienBoss){ 
     // User touch alienBoss, do whatever you want 
    } 
} 

然后,您要启用声音s o您可以使用AVAudioPlayer库:

import UIKit 
    import AVFoundation 

    class ViewController: UIViewController { 

     var player:AVAudioPlayer = AVAudioPlayer() 

override func viewDidLoad() { 
     super.viewDidLoad() 

     let audioPath = NSBundle.mainBundle().pathForResource("alienSound", ofType: "mp3") 
     var error:NSError? = nil 
} 

您可以使用下面的代码来播放声音,停止,音量设置:

do { 
    player = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!)) 
    // to play the mp3 
    player.play() 
    // to stop it 
    player.stop() 
    // to pause it 
    player.pause() 
    // to set the volume 
    player.volume = 0.5 // from 0.0 to 1.0 
    ... 
} 
catch { 
    print("Something bad happened.") 
} 
+0

非常感谢,我非常感谢您花时间为我做这件事。只要我吃完了,我会去尝试一切,并回报:) 非常善良的你来帮忙。 –

+0

没问题。欢迎来到StackOverflow。最好谢谢谁提出一个问题是作出投票或接受答案作为这个问题的正确答案 –

+0

好吧,我做了一些测试,我可能做错了,因为我得到一个错误。覆盖func touchesBegan(触及:NSSet,withEvent事件:UIEvent){
let touch = touches.first as! UITouch
如果(touch.view == imgAlien){
sfxPop.play()
}
}

我得到的错误信息:方法不覆盖任何方法从它的超
我还得到消息: 'NSSet'类型的值首先没有成员