PickerViewで指定した回数分カウントダウンさせたいのですが、
swift
1 var cnt:Int = 60*(//ここにpickerViewで指定した回数再生させる) 2
この部分の「指定した回数を×60」させるコードがわかりません。
どのようにしたら良いでしょうか?
また、可能であればカウントダウン開始のコードもわからないので教えていただけたらと思います。
※最終形はPickerViewで分数を指定してカウントダウンをさせるというものです。
何卒よろしくお願いいたします。
Swift
1 2import UIKit 3import AVFoundation 4 5class TTViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate { 6 7 @IBOutlet weak var lbTimer: UILabel! 8 let noodleTime: TimeInterval = 60 * 1 // 時間 60秒×50 9 10 var cnt:Int = 60*(//ここにpickerViewで指定した回数再生させる) 11 12 var timer:Timer! 13 14 15 16 @IBAction func stopButton(){ 17 audioPlayer.stop() 18 } 19 var audioPlayer : AVAudioPlayer! 20 21 @IBAction func playButton(){ 22 audioPlayer.play() 23 } 24 25 @IBOutlet var clapPickerView: UIPickerView! 26 var soundCount: Int = 0 27 28 29 30 31 override func viewDidLoad() { 32 33 super.viewDidLoad() 34 //略 35 clapPickerView.delegate = self 36 clapPickerView.dataSource = self 37 38 // Do any additional setup after loading the view, typically from a nib. 39 40 41 //再生する音源のURLを生成 42 let soundFilePath = Bundle.main.path(forResource: "bell",ofType:"mp3")! 43 let fileURL = URL(fileURLWithPath: soundFilePath) 44 //AVAudioPlayerのインスタンス化 45 do{ 46 audioPlayer = try AVAudioPlayer(contentsOf: fileURL) 47 } 48 catch{ 49 print("音声ファイルの読み込みに失敗しました。") 50 } 51 52 53 timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.update), userInfo: nil, repeats: true) 54 timer.fire() 55 } 56 57 //いくつカテゴリーを持つか 58 func numberOfComponents(in pickerView: UIPickerView) -> Int { 59 return 1 60 61 } 62 //いくつ選択肢を作るか 63 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 64 return 100 65 //上記の100が回数を示している 66 } 67 //選択肢に何を表示していくか 68 func pickerView(_ pickerView: UIPickerView, titleForRow row:Int,forComponent component: Int) -> String{ 69 return "(row+1)分" 70 } 71 //指定時間分をカウントダウンする 72 func pickerView(_ pickerView: UIPickerView, didSelectRow row:Int,inComponent component: Int) { 73 soundCount = row 74 } 75 76 override func didReceiveMemoryWarning() { 77 super.didReceiveMemoryWarning() 78 // Dispose of any resources that can be recreated. 79 } 80 81 func update(){ 82 cnt -= 1 83 84 lbTimer.text = String(cnt/60)+":"+String(cnt%60) 85 86 if (cnt == 0) { 87 timer.invalidate() 88 audioPlayer.play() 89 90 // アラートを作成 91 let alert = UIAlertController( 92 title: "時間になりました", 93 message: "(row)分の試験が終了しました。", 94 preferredStyle: .alert) 95 96 // アラートにボタンをつける 97 alert.addAction(UIAlertAction(title: "確認", style: .default)) 98 99 // アラート表示 100 self.present(alert, animated: true, completion: nil) 101 } 102 } 103 104 105 /* 106 // MARK: - Navigation 107 108 // In a storyboard-based application, you will often want to do a little preparation before navigation 109 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 110 // Get the new view controller using segue.destinationViewController. 111 // Pass the selected object to the new view controller. 112 } 113 */ 114 115} 116
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。