質問編集履歴
3
制約追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -251,3 +251,11 @@
|
|
251
251
|
|
252
252
|
|
253
253
|
```
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
## 追記2
|
258
|
+
|
259
|
+
すでにチュートリアルを完成させてしまった為同じ構成ではないかもしれませんが、Constraintsの構成を追記します。
|
260
|
+
|
261
|
+
![イメージ説明](d5e574552aff23a9e20dc48698991204.png)
|
2
コメント削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,22 +30,6 @@
|
|
30
30
|
|
31
31
|
```
|
32
32
|
|
33
|
-
//
|
34
|
-
|
35
|
-
// RatingControl.swift
|
36
|
-
|
37
|
-
// bws
|
38
|
-
|
39
|
-
//
|
40
|
-
|
41
|
-
// Created by BSL on 2019/03/15.
|
42
|
-
|
43
|
-
// Copyright © 2019年 hrkbyc. All rights reserved.
|
44
|
-
|
45
|
-
//
|
46
|
-
|
47
|
-
|
48
|
-
|
49
33
|
import UIKit
|
50
34
|
|
51
35
|
|
1
カスタムクラスを使用していることを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,3 +15,255 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
![イメージ説明](3dde0748058eb50407cd3eb4bf8065e9.png)
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
## 追記
|
24
|
+
|
25
|
+
星はRatingControlというカスタムクラスで表示しています。
|
26
|
+
|
27
|
+
![イメージ説明](0ff0686305b538069d33597b17252f83.png)
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
//
|
34
|
+
|
35
|
+
// RatingControl.swift
|
36
|
+
|
37
|
+
// bws
|
38
|
+
|
39
|
+
//
|
40
|
+
|
41
|
+
// Created by BSL on 2019/03/15.
|
42
|
+
|
43
|
+
// Copyright © 2019年 hrkbyc. All rights reserved.
|
44
|
+
|
45
|
+
//
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
import UIKit
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
@IBDesignable class RatingControl: UIStackView {
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
/*
|
58
|
+
|
59
|
+
// Only override draw() if you perform custom drawing.
|
60
|
+
|
61
|
+
// An empty implementation adversely affects performance during animation.
|
62
|
+
|
63
|
+
override func draw(_ rect: CGRect) {
|
64
|
+
|
65
|
+
// Drawing code
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
*/
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
//MARK: Properties
|
74
|
+
|
75
|
+
private var ratingButtons = [UIButton]()
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
var rating = 0 {
|
80
|
+
|
81
|
+
didSet {
|
82
|
+
|
83
|
+
updateButtonSelectionStates()
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
@IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0) {
|
92
|
+
|
93
|
+
didSet {
|
94
|
+
|
95
|
+
setupButtons()
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
@IBInspectable var starCount: Int = 5 {
|
104
|
+
|
105
|
+
didSet {
|
106
|
+
|
107
|
+
setupButtons()
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
//MARK: Initialization(初期化)
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
override init(frame: CGRect) {
|
120
|
+
|
121
|
+
super.init(frame: frame)
|
122
|
+
|
123
|
+
setupButtons()
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
required init(coder: NSCoder) {
|
130
|
+
|
131
|
+
super.init(coder: coder)
|
132
|
+
|
133
|
+
setupButtons()
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
//MARK: Private Methods
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
private func setupButtons() {
|
144
|
+
|
145
|
+
for button in ratingButtons {
|
146
|
+
|
147
|
+
removeArrangedSubview(button)
|
148
|
+
|
149
|
+
button.removeFromSuperview()
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
ratingButtons.removeAll()
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
//ボタン画像読み込み
|
158
|
+
|
159
|
+
let bundle = Bundle(for: type(of: self))
|
160
|
+
|
161
|
+
let filledStar = UIImage(named: "filledStar", in: bundle, compatibleWith: self.traitCollection)
|
162
|
+
|
163
|
+
let emptyStar = UIImage(named:"emptyStar", in: bundle, compatibleWith: self.traitCollection)
|
164
|
+
|
165
|
+
let highlightedStar = UIImage(named:"highlightedStar", in: bundle, compatibleWith: self.traitCollection)
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
for _ in 0..<starCount {
|
170
|
+
|
171
|
+
// ボタン作成
|
172
|
+
|
173
|
+
let button = UIButton()
|
174
|
+
|
175
|
+
button.setImage(emptyStar, for: .normal)
|
176
|
+
|
177
|
+
button.setImage(filledStar, for: .selected)
|
178
|
+
|
179
|
+
button.setImage(highlightedStar, for: .highlighted)
|
180
|
+
|
181
|
+
button.setImage(highlightedStar, for: [.highlighted, .selected])
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
// 自動生成された制約を削除
|
186
|
+
|
187
|
+
button.translatesAutoresizingMaskIntoConstraints = false
|
188
|
+
|
189
|
+
// 大きさを指定
|
190
|
+
|
191
|
+
button.heightAnchor.constraint(equalToConstant: starSize.height).isActive = true
|
192
|
+
|
193
|
+
button.widthAnchor.constraint(equalToConstant: starSize.width).isActive = true
|
194
|
+
|
195
|
+
// ボタンアクションを設定
|
196
|
+
|
197
|
+
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)
|
198
|
+
|
199
|
+
// Add the button to the stack
|
200
|
+
|
201
|
+
addArrangedSubview(button)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
// Add the new button to the rating button array
|
206
|
+
|
207
|
+
ratingButtons.append(button)
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
updateButtonSelectionStates()
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
//MARK: Button Action
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
@objc func ratingButtonTapped(button: UIButton) {
|
224
|
+
|
225
|
+
print("Button pressed ????")
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
guard let index = ratingButtons.index(of: button) else {
|
230
|
+
|
231
|
+
fatalError("The button, (button), is not in the ratingButtons array: (ratingButtons)")
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
let selectedRating = index + 1
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
if selectedRating == rating {
|
242
|
+
|
243
|
+
rating = 0
|
244
|
+
|
245
|
+
} else {
|
246
|
+
|
247
|
+
rating = selectedRating
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
private func updateButtonSelectionStates() {
|
256
|
+
|
257
|
+
for (index, button) in ratingButtons.enumerated() {
|
258
|
+
|
259
|
+
button.isSelected = index < rating
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
```
|