【问题标题】:How to perform action on simple UIView Animation如何对简单的 UIView 动画执行操作
【发布时间】:2016-05-01 14:25:55
【问题描述】:

直到几周前,我才进行过任何编码,虽然我自己已经取得了合理的进展,但我正在努力解决一些我希望有人不会介意帮助的事情。

我正在尝试用 Swift(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()

}

}

【问题讨论】:

  • 如果信息不足,我们深表歉意,如果需要,我很乐意发布更多信息,如果不需要,我不想提供太多信息:)
  • 我在这里没有看到任何与图像相关的代码.. 只是子类的移动。如果确实是外星人类,请告诉我们,以便我们回答
  • 听起来您想做的事情是使用 Sprite 套件开箱即用的。您可能想查看 Sprite 套件教程,看看是否值得稍微更改您的实现
  • 大家好,很抱歉没有说得更清楚。在 ViewController.swift 文件中,我创建了“AlienAnimations”类型的 IBOutlets,这就是我所说的包含动画的类。然后我将图像指定为故事板中图像的自定义类 (AlienAnimations)。如果需要,我可以编辑我的原始帖子并显示所有代码。抱歉,第一次在这里发帖,我是编码方面的超级新手!

标签: ios swift animation uiimageview


【解决方案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
    }
}

然后,您要启用声音以便可以使用 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.")
}

【讨论】:

  • 非常感谢,非常感谢您花时间为我做这件事。等我吃完,我就去试一试,然后回来报告:) 非常感谢你的帮助。
  • 没问题。欢迎来到 StackOverflow。提出问题的人最好的感谢是投票或接受答案作为该问题的正确答案
  • 好的,我做了一些测试,我可能做错了什么,因为我遇到了错误。 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    if(touch.view == imgAlien) {
    sfxPop.play()
    }
    }

    我收到错误消息:方法执行不要覆盖它的超类中的任何方法
    我也收到消息:'NSSet'类型的值首先没有成员
  • 我已经尝试查看文档,但如果我们在函数覆盖声明之后发表声明,我看不出有什么问题。对不起,如果我是愚蠢的,我的大脑并不总是在逻辑上与代码一起工作,这一切都是那么新:S
  • 移除覆盖词
猜你喜欢
  • 2020-09-03
  • 1970-01-01
  • 2012-05-29
  • 2015-11-06
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
相关资源
最近更新 更多