回答編集履歴
1
修正
answer
CHANGED
@@ -5,4 +5,121 @@
|
|
5
5
|
|
6
6
|
// Run loopに登録する
|
7
7
|
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
|
8
|
+
```
|
9
|
+
|
10
|
+
|
11
|
+
確認したコード
|
12
|
+
---
|
13
|
+
|
14
|
+
```swift
|
15
|
+
import UIKit
|
16
|
+
|
17
|
+
class KenteiViewController: UIViewController {
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
//制限時間の数字表示
|
22
|
+
private var timerLabel: UILabel!
|
23
|
+
//制限時間バー
|
24
|
+
private var myImageView: UIImageView!
|
25
|
+
|
26
|
+
var timer = NSTimer()
|
27
|
+
var countNum = 0
|
28
|
+
|
29
|
+
// スクリーン画面のサイズを取得
|
30
|
+
let scWid: CGFloat = UIScreen.mainScreen().bounds.width //画面の幅
|
31
|
+
let scHei: CGFloat = UIScreen.mainScreen().bounds.height //画面の高さ
|
32
|
+
|
33
|
+
//制限時間の表示
|
34
|
+
func timerLimit(){
|
35
|
+
|
36
|
+
// UILabelを作成する.
|
37
|
+
//timerLabel = UILabel(frame: CGRectMake(scWid*0.79 ,scHei*0.4375 ,scWid*0.11 ,scHei*0.025))
|
38
|
+
timerLabel = UILabel(frame: CGRectMake(100 ,200 ,scWid*0.11 ,scHei*0.025))
|
39
|
+
timerLabel.font = UIFont.boldSystemFontOfSize(scHei*0.018)
|
40
|
+
timerLabel.textColor = UIColor.whiteColor()
|
41
|
+
timerLabel.backgroundColor = UIColor.redColor()
|
42
|
+
self.view.addSubview(timerLabel)
|
43
|
+
print(timerLabel)
|
44
|
+
}
|
45
|
+
|
46
|
+
func update() {
|
47
|
+
countNum += 1
|
48
|
+
timeFormat(countNum)
|
49
|
+
}
|
50
|
+
|
51
|
+
//制限時間の数字の表示
|
52
|
+
func timeFormat(countNum:Int) {
|
53
|
+
|
54
|
+
let ms = countNum % 100
|
55
|
+
let s = countNum / 100
|
56
|
+
timerLabel.text = String(format: "%02d.%02d", 19-s, 100-ms)
|
57
|
+
|
58
|
+
if Float(timerLabel.text!) <= 0 {
|
59
|
+
timerLabel.text = "0.00"
|
60
|
+
timerStop()
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
//制限時間追加メソッド
|
65
|
+
func timermethod(){
|
66
|
+
countNum = 0
|
67
|
+
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: #selector(KenteiViewController.update), userInfo: nil, repeats: true)
|
68
|
+
// Run loopに登録する
|
69
|
+
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
|
70
|
+
}
|
71
|
+
|
72
|
+
//制限時間を止めるメソッド
|
73
|
+
func timerStop(){
|
74
|
+
timer.invalidate()
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
//制限時間バー(オレンジ色のバー)の表示
|
80
|
+
func timerbar(){
|
81
|
+
|
82
|
+
let barHeight = scHei*0.02
|
83
|
+
let barWidth = scWid*0.66
|
84
|
+
let barXPosition = scWid*0.08
|
85
|
+
let barYPosition = scHei*0.44
|
86
|
+
let barXPositionEnd = barXPosition + barWidth
|
87
|
+
// UIImageViewを作成する.
|
88
|
+
myImageView = UIImageView()
|
89
|
+
// 表示する画像を設定する.
|
90
|
+
let myImage = UIImage(named: "timebar.png")
|
91
|
+
|
92
|
+
// 画像をUIImageViewに設定する.
|
93
|
+
myImageView.image = myImage
|
94
|
+
myImageView.backgroundColor = UIColor.blueColor()
|
95
|
+
// 画像の表示する座標を指定する.
|
96
|
+
myImageView.frame = CGRectMake(barXPosition ,barYPosition ,barWidth ,barHeight)
|
97
|
+
// UIImageViewをViewに追加する.
|
98
|
+
self.view.addSubview(myImageView)
|
99
|
+
|
100
|
+
print(myImageView)
|
101
|
+
|
102
|
+
UIView.animateWithDuration(20, delay: 0.0, options : UIViewAnimationOptions.CurveLinear, animations: {() -> Void in
|
103
|
+
self.myImageView.frame = CGRectMake(barXPositionEnd, barYPosition, 0, barHeight)
|
104
|
+
},
|
105
|
+
completion: {(finished: Bool) -> Void in
|
106
|
+
// アニメーション終了後の処理
|
107
|
+
print("制限時間バーは\(finished)")
|
108
|
+
|
109
|
+
|
110
|
+
})
|
111
|
+
|
112
|
+
}
|
113
|
+
|
114
|
+
override func viewDidLoad() {
|
115
|
+
super.viewDidLoad()
|
116
|
+
|
117
|
+
//制限時間ラベルの表示
|
118
|
+
timerLimit()
|
119
|
+
//制限時間をつけるメソッドを呼び出す
|
120
|
+
timermethod()
|
121
|
+
|
122
|
+
timerbar()
|
123
|
+
}
|
124
|
+
}
|
8
125
|
```
|