2016-05-01 98 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'類型的值首先沒有成員