質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1763閲覧

画像をタップした際のタイマー停止

ZY.

総合スコア22

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/01/30 10:28

編集2019/01/30 12:10

画像をタップしたときにタイマーを停止させたい

ここに質問の内容を詳しく書いてください。
タイマーで画像を切り替える機能をつけたスライドショーを作っています。
画像をタップした際に画面を切り替えると同時にタイマーをストップさせたいのですがうまくいきません。
コードがうまく動作しているのか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. } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hameji001

2019/01/30 11:42

何が、何に繋げてあるのかわかりません。
ZY.

2019/01/30 12:10

2つ目のViewControllerのコードを追加しました。よろしくおねがいします。
guest

回答1

0

ベストアンサー

@IBAction func tapImage(_ sender: Any) {
if self.timer != nil { ← ★ここにブレークポイントを張る★

↑ここにブレークポイントを貼って画像をタップしてブレークするのであれば、
ステップオーバーするなりして、コードが一行ずつ実行される様子を見て期待する結果と違うところが無いかをみてみてください。

ブレークしないのであれば、アクションがうまく紐付いていない可能性を疑ってください。

投稿2019/02/04 02:30

takabosoft

総合スコア8356

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問