ヒロポメと申します。アプリを作っています。
諸々調べたのですが解決せず、お力添え頂けますと幸いです。。。!
お手数ですが、よろしくお願いいたします。
●やりたいこと
collection view内で、丸いボタンのセルを押したら、音声が流れると同時に、丸ボタンが拡大・縮小を繰り返すアニメーションを実行させたいです。
●問題
先に丸が拡大縮小をして、アニメーションが終了して静止したあとに、音声が流れます。
アニメーションと、音声の再生を同時に行うにはどうすれば良いでしょうか?
Swift
1 2 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 3 4//中略 5 6 animatePulsatingLayer() 7 8 URLSession.shared.dataTask(with: audioUrl!, completionHandler: { (data, response, error) in 9 10 DispatchQueue.global().async{ 11 DispatchQueue.main.async{ 12 do { 13 self.audioPlayer = try AVAudioPlayer(data:data!) 14 self.audioPlayer.delegate = self 15 self.audioPlayer.play() 16 self.myCollectionView.reloadData(); 17 } 18 catch{} 19 } 20 } 21 }).resume() 22 23} 24
Swift
1 private func animatePulsatingLayer() { 2 let animation = CABasicAnimation(keyPath: "transform.scale") 3 animation.toValue = 1.4 //丸がどこまで大きくなるか。1がデフォルト。 4 animation.duration = 0.8 //動く速さ 5 animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) 6 animation.autoreverses = true //元に戻る 7 animation.repeatCount = Float.infinity 8 layer2?.add(animation, forKey: "pulsing") 9 } 10
ちなみにanimatePulsatingLayer関数を、下記do{}の中にかくと、音声だけが流れて、アニメーションは機能しなくなりました。
Swift
1 //再掲 2 do { 3 self.audioPlayer = try AVAudioPlayer(data:data!) 4 self.audioPlayer.delegate = self 5 self.audioPlayer.play() 6 self.animatePulsatingLayer() //ここに挿入してみる 7 self.myCollectionView.reloadData(); 8 }
また、音声の再生が終了したら、アニメーションも止めるようにするにはどうすればいいでしょうか?
以下関数でなんとかできないでしょうか?
以下、func audioPlayerDidFinishPlayingを加えた修正版です。
最初の投稿で「中略」として記載していなかった箇所も記載しました。
Swift
1 func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {}
Swift
1 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 2 if let cell = collectionView.cellForItem(at: indexPath) as? ImageCollectionViewCell { 3 cell.didSelect(indexPath: indexPath) 4 layer2 = cell.imgImg.layer 5 } 6 7 number = indexPath.row 8 9 //押したら、fiebase上のlistenedがtrueに変わる。 10 let newValuesForProfile = ["listened": true] 11 if messages[indexPath.row].senderId == self.toID { 12 DBProvider.Instance.dbRef.child("Media_Messages").child(messages[indexPath.row].childkey!).updateChildValues(newValuesForProfile) 13 } 14 15 animatePulsatingLayer()//アニメーションの詳細を記述した関数を呼び出す 16 17 do{ 18 let message = messages[indexPath.row] 19 let audioUrl = URL(string: message.url!) 20 URLSession.shared.dataTask(with: audioUrl!, completionHandler: { (data, response, error) in 21 22 DispatchQueue.global().async{ 23 DispatchQueue.main.async{ 24 25 func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { 26 27 do { 28 self.audioPlayer = try AVAudioPlayer(data:data!) 29 self.audioPlayer.delegate = self 30 self.audioPlayer.play() 31 self.myCollectionView.reloadData(); 32 } 33 catch{} 34 } //func audioPlayerDidFinishPlaying終了 35 } 36 } 37 38 }).resume() 39 40 } catch { 41 } 42 43 } 44 45 46
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/04 15:11