teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

codeの修正

2020/08/10 04:27

投稿

Narikura
Narikura

スコア6

title CHANGED
File without changes
body CHANGED
@@ -24,7 +24,7 @@
24
24
  @IBOutlet weak var paperbutton: UIButton!
25
25
  @IBOutlet weak var rockbutton: UIButton!
26
26
 
27
- var timer: Timer!
27
+ var timer: Timer?
28
28
  var nowIndex:Int = 0
29
29
 
30
30
  enum hand: Int {
@@ -54,18 +54,16 @@
54
54
  return
55
55
  }
56
56
 
57
- Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) {_ in
58
- self.janken(youHand: youHand, cpHand: cpHand);
59
- self.result(youHand: youHand, cpHand: cpHand)
60
- }
61
-
62
- if (timer == nil) {
57
+ if timer == nil {
63
58
  timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(changeImage), userInfo: nil, repeats: true)
64
59
  }
65
60
  DispatchQueue.main.asyncAfter(deadline: .now() + 1.99) {
66
- self.timer.invalidate()
61
+ self.timer?.invalidate()
62
+ self.janken(youHand: youHand, cpHand: cpHand)
63
+ self.result(youHand: youHand, cpHand: cpHand)
64
+ self.timer = nil
67
- }
65
+ }
68
- }
66
+ }
69
67
 
70
68
  @objc func changeImage() {
71
69
  nowIndex += 1
@@ -78,7 +76,7 @@
78
76
  func janken(youHand: hand, cpHand: hand) {
79
77
  if youHand.rawValue == cpHand.rawValue {
80
78
  janken.text = "you draw"
81
- }else if (youHand.rawValue - cpHand.rawValue + 3)%3 == cpHand.rawValue {
79
+ }else if (youHand.rawValue + 1)%3 == cpHand.rawValue {
82
80
  janken.text = "you win!"
83
81
  }else {
84
82
  janken.text = "you lose"
@@ -94,9 +92,6 @@
94
92
  self.cpHand.image = UIImage(named: "paperpic")
95
93
  }
96
94
  }
97
-
98
-
99
-
100
-
101
95
  }
96
+
102
97
  ```

2

codeの修正

2020/08/10 04:27

投稿

Narikura
Narikura

スコア6

title CHANGED
File without changes
body CHANGED
@@ -12,6 +12,7 @@
12
12
  ### 該当のソースコード
13
13
 
14
14
  ```Swift
15
+
15
16
  import UIKit
16
17
 
17
18
  final class ViewController: UIViewController {
@@ -53,7 +54,7 @@
53
54
  return
54
55
  }
55
56
 
56
- Timer.scheduledTimer(withTimeInterval: 2, repeats: false) {_ in
57
+ Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) {_ in
57
58
  self.janken(youHand: youHand, cpHand: cpHand);
58
59
  self.result(youHand: youHand, cpHand: cpHand)
59
60
  }
@@ -77,7 +78,7 @@
77
78
  func janken(youHand: hand, cpHand: hand) {
78
79
  if youHand.rawValue == cpHand.rawValue {
79
80
  janken.text = "you draw"
80
- }else if (youHand.rawValue + 1)%3 == cpHand.rawValue {
81
+ }else if (youHand.rawValue - cpHand.rawValue + 3)%3 == cpHand.rawValue {
81
82
  janken.text = "you win!"
82
83
  }else {
83
84
  janken.text = "you lose"
@@ -90,7 +91,7 @@
90
91
  } else if cpHand.rawValue == 1 {
91
92
  self.cpHand.image = UIImage(named: "scissorspic")
92
93
  } else if cpHand.rawValue == 2 {
93
- self.cpHand.image = UIImage(named: "scissorspic")
94
+ self.cpHand.image = UIImage(named: "paperpic")
94
95
  }
95
96
  }
96
97
 
@@ -98,4 +99,4 @@
98
99
 
99
100
 
100
101
  }
101
- ``
102
+ ```

1

問題点が変わったため変更しました。

2020/08/09 13:34

投稿

Narikura
Narikura

スコア6

title CHANGED
@@ -1,1 +1,1 @@
1
- ジャンケンアプリのtimer設定
1
+ ジャンケンアプリの作成
body CHANGED
@@ -5,7 +5,8 @@
5
5
 
6
6
  ### 発生している問題・エラーメッセージ
7
7
 
8
+ 2回目以降のジャンケン結果がおかしくなる。
8
- 1回の勝敗につき1回のボタンしか押さないた、グルグルさせていた画像ボタンを2回押さいと止まりません
9
+ 自分押した手とcpの出力された手の正しい勝敗がlabelprintない。また、2回目以降imageViewのグルグルがなくなる
9
10
 
10
11
 
11
12
  ### 該当のソースコード
@@ -23,8 +24,6 @@
23
24
  @IBOutlet weak var rockbutton: UIButton!
24
25
 
25
26
  var timer: Timer!
26
- let timerInterval = 0.05
27
- let timelag = 2.0
28
27
  var nowIndex:Int = 0
29
28
 
30
29
  enum hand: Int {
@@ -61,10 +60,10 @@
61
60
 
62
61
  if (timer == nil) {
63
62
  timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(changeImage), userInfo: nil, repeats: true)
64
- } else {
65
- timer.invalidate()
66
- timer = nil
67
63
  }
64
+ DispatchQueue.main.asyncAfter(deadline: .now() + 1.99) {
65
+ self.timer.invalidate()
66
+ }
68
67
  }
69
68
 
70
69
  @objc func changeImage() {
@@ -94,14 +93,9 @@
94
93
  self.cpHand.image = UIImage(named: "scissorspic")
95
94
  }
96
95
  }
96
+
97
+
98
+
99
+
97
100
  }
98
-
99
-
100
- ```
101
+ ``
101
-
102
-
103
-
104
- ### 補足情報(FW/ツールのバージョンなど)
105
-
106
- 補足情報ではないですが、timerを使用して画像(✊✌️✋)をグルグルさせているので、n秒後に勝手に止まるようなシステムがあるのであれば教えていただきたいです。
107
- もちろん代替案でも