简体   繁体   English

如何在简单的 UIView 动画上执行操作

[英]How to perform action on simple UIView Animation

I've never done any coding before until a few weeks ago, and whilst I've made reasonable progress on my own, I'm struggling with something that I hope someone wouldn't mind helping with.直到几周前,我之前从未做过任何编码,虽然我自己取得了合理的进步,但我正在努力解决一些我希望有人不介意帮助的事情。

I'm trying to make a simple app for my daughter in Swift (xCode Version 7.3 - 7D175).我正在尝试用 Swift(xCode 版本 7.3 - 7D175)为我的女儿制作一个简单的应用程序。 It's a series of animations where characters pop up and down inside a moon crater.这是一系列动画,角色在月球陨石坑内上下弹出。

What I want to be able to do, is call a function when one of the animated UIImages is pressed.我想要做的是在按下动画 UIImages 之一时调用一个函数。 Ideally I'd like it to play a sound when the animation is pressed (I know how to call sounds, just not in this way)理想情况下,我希望它在按下动画时播放声音(我知道如何调用声音,只是不是这样)

I've created a Class where I wrote the code for the animations我创建了一个类,我在其中编写了动画代码

  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)

    }

If someone could point me in the right direction, I'd greatly appreciated.如果有人能指出我正确的方向,我将不胜感激。 I guess there's probably better ways of animating, but this is all I know at the moment.我想可能有更好的动画方法,但这就是我目前所知道的。

EDIT - 2nd May编辑 - 5 月 2 日

OK so I eventually got it working...well, nearly :) It still doesn't work exactly as I'd like, but I'm working on it.好的,所以我最终让它工作了......好吧,几乎 :) 它仍然不能像我想要的那样工作,但我正在努力。

The animations essentially move vertically along a path (so the aliens pop up like they're coming out of a hole).动画基本上是沿着一条路径垂直移动(所以外星人就像从洞里出来一样弹出)。 The below code works, but still allows you to click on the image at the start of it's path when it's effectively down a hole.下面的代码有效,但仍然允许您在它有效地进入一个洞时在它的路径开始处单击图像。

I still need to work out how to click where it actually is now, and not be able to press its starting point.我仍然需要弄清楚如何点击它现在的实际位置,并且无法按下它的起点。

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()

}

} }

Supposing your have your planet and animation working up and down as you wish, what do you do is to detecting touch:假设您的星球和动画按您的意愿上下工作,您要做的是检测触摸:

This is just an example:这只是一个例子:

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
    }
}

Then, you want to enable sound so you can use AVAudioPlayer library:然后,您想要启用声音,以便您可以使用 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
}

You can use the code below to play a sound, stop, set the volume:您可以使用以下代码播放声音、停止、设置音量:

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.")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM