画像をタップしたときにタイマーを停止させたい
ここに質問の内容を詳しく書いてください。
タイマーで画像を切り替える機能をつけたスライドショーを作っています。
画像をタップした際に画面を切り替えると同時にタイマーをストップさせたいのですがうまくいきません。
コードがうまく動作しているのかprint文を用いて確認してみたところうまくprintが動作されませんでした。
該当のソースコード
Swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 @IBOutlet weak var button: UIButton! 6 7 @IBOutlet weak var topImage: UIImageView! 8 9 @IBOutlet weak var backButton: UIButton! 10 11 @IBOutlet weak var nextButton: UIButton! 12 13 var imageIndex = 0 14 15 var timer: Timer! 16 var timer_sec: Float = 0 17 18 let images = [UIImage(named: "imageA"), UIImage(named: "imageB"), UIImage(named: "imageC"),UIImage(named: "imageD")] 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 // Do any additional setup after loading the view, typically from a nib. 23 24 let imageView = images[0] 25 26 topImage.image = imageView 27 } 28 29 override func didReceiveMemoryWarning() { 30 super.didReceiveMemoryWarning() 31 // Dispose of any resources that can be recreated. 32 } 33 34 @IBAction func backImage(_ sender: Any) { 35 if imageIndex == 0 { 36 imageIndex = 3 37 } else { 38 imageIndex -= 1 39 } 40 topImage.image = images[imageIndex] 41 } 42 43 @IBAction func nextImage(_ sender: Any) { 44 if imageIndex == 3 { 45 imageIndex = 0 46 } else { 47 imageIndex += 1 48 } 49 topImage.image = images[imageIndex] 50 } 51 52 @IBAction func startStop(_ sender: Any) { 53 if self.timer == nil { 54 self.timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(onTimer(_:)), userInfo: nil, repeats: true) 55 button.setTitle("一時停止", for: .normal) 56 backButton.isEnabled = false 57 nextButton.isEnabled = false 58 } else { 59 if self.timer == nil { 60 self.timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(onTimer(_:)), userInfo: nil, repeats: true) 61 } else { 62 if timer.isValid == true { 63 timer.invalidate() 64 button.setTitle("再生", for: .normal) 65 backButton.isEnabled = true 66 nextButton.isEnabled = true 67 } else { 68 self.timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(onTimer(_:)), userInfo: nil, repeats: true) 69 button.setTitle("一時停止", for: .normal) 70 backButton.isEnabled = false 71 nextButton.isEnabled = false 72 } 73 } 74 } 75 } 76 77 @objc func onTimer(_ timer: Timer) { 78 if imageIndex == 2 { 79 imageIndex = 0 80 } else { 81 imageIndex += 1 82 } 83 topImage.image = images[imageIndex] 84 } 85 86 @IBAction func tapImage(_ sender: Any) { 87 if self.timer != nil { 88 print("タイマー停止") 89 button.setTitle("再生", for: .normal) 90 backButton.isEnabled = true 91 nextButton.isEnabled = true 92 timer.invalidate() 93 self.timer = nil 94 } 95 performSegue(withIdentifier: "toSecondVC", sender: nil) 96 } 97 98 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 99 let secondViewController:SecondViewController = segue.destination as! SecondViewController 100 101 secondViewController.image = topImage.image! 102 } 103 104 @IBAction func unwind(_ segue: UIStoryboardSegue) { 105 } 106 107}
import UIKit class SecondViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! var image:UIImage = UIImage() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. imageView.image = image } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
回答1件
あなたの回答
tips
プレビュー