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

質問編集履歴

3

制約追記

2019/03/18 05:34

投稿

bws
bws

スコア98

title CHANGED
File without changes
body CHANGED
@@ -124,4 +124,8 @@
124
124
  }
125
125
  }
126
126
 
127
- ```
127
+ ```
128
+
129
+ ## 追記2
130
+ すでにチュートリアルを完成させてしまった為同じ構成ではないかもしれませんが、Constraintsの構成を追記します。
131
+ ![イメージ説明](d5e574552aff23a9e20dc48698991204.png)

2

コメント削除

2019/03/18 05:33

投稿

bws
bws

スコア98

title CHANGED
File without changes
body CHANGED
@@ -14,14 +14,6 @@
14
14
  ![イメージ説明](0ff0686305b538069d33597b17252f83.png)
15
15
 
16
16
  ```
17
- //
18
- // RatingControl.swift
19
- // bws
20
- //
21
- // Created by BSL on 2019/03/15.
22
- // Copyright © 2019年 hrkbyc. All rights reserved.
23
- //
24
-
25
17
  import UIKit
26
18
 
27
19
  @IBDesignable class RatingControl: UIStackView {

1

カスタムクラスを使用していることを追記

2019/03/18 05:27

投稿

bws
bws

スコア98

title CHANGED
File without changes
body CHANGED
@@ -6,4 +6,130 @@
6
6
 
7
7
  ![イメージ説明](0f1daf8612c87800f054da8c463c7f72.png)
8
8
 
9
- ![イメージ説明](3dde0748058eb50407cd3eb4bf8065e9.png)
9
+ ![イメージ説明](3dde0748058eb50407cd3eb4bf8065e9.png)
10
+
11
+
12
+ ## 追記
13
+ 星はRatingControlというカスタムクラスで表示しています。
14
+ ![イメージ説明](0ff0686305b538069d33597b17252f83.png)
15
+
16
+ ```
17
+ //
18
+ // RatingControl.swift
19
+ // bws
20
+ //
21
+ // Created by BSL on 2019/03/15.
22
+ // Copyright © 2019年 hrkbyc. All rights reserved.
23
+ //
24
+
25
+ import UIKit
26
+
27
+ @IBDesignable class RatingControl: UIStackView {
28
+
29
+ /*
30
+ // Only override draw() if you perform custom drawing.
31
+ // An empty implementation adversely affects performance during animation.
32
+ override func draw(_ rect: CGRect) {
33
+ // Drawing code
34
+ }
35
+ */
36
+
37
+ //MARK: Properties
38
+ private var ratingButtons = [UIButton]()
39
+
40
+ var rating = 0 {
41
+ didSet {
42
+ updateButtonSelectionStates()
43
+ }
44
+ }
45
+
46
+ @IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0) {
47
+ didSet {
48
+ setupButtons()
49
+ }
50
+ }
51
+
52
+ @IBInspectable var starCount: Int = 5 {
53
+ didSet {
54
+ setupButtons()
55
+ }
56
+ }
57
+
58
+ //MARK: Initialization(初期化)
59
+
60
+ override init(frame: CGRect) {
61
+ super.init(frame: frame)
62
+ setupButtons()
63
+ }
64
+
65
+ required init(coder: NSCoder) {
66
+ super.init(coder: coder)
67
+ setupButtons()
68
+ }
69
+
70
+ //MARK: Private Methods
71
+
72
+ private func setupButtons() {
73
+ for button in ratingButtons {
74
+ removeArrangedSubview(button)
75
+ button.removeFromSuperview()
76
+ }
77
+ ratingButtons.removeAll()
78
+
79
+ //ボタン画像読み込み
80
+ let bundle = Bundle(for: type(of: self))
81
+ let filledStar = UIImage(named: "filledStar", in: bundle, compatibleWith: self.traitCollection)
82
+ let emptyStar = UIImage(named:"emptyStar", in: bundle, compatibleWith: self.traitCollection)
83
+ let highlightedStar = UIImage(named:"highlightedStar", in: bundle, compatibleWith: self.traitCollection)
84
+
85
+ for _ in 0..<starCount {
86
+ // ボタン作成
87
+ let button = UIButton()
88
+ button.setImage(emptyStar, for: .normal)
89
+ button.setImage(filledStar, for: .selected)
90
+ button.setImage(highlightedStar, for: .highlighted)
91
+ button.setImage(highlightedStar, for: [.highlighted, .selected])
92
+
93
+ // 自動生成された制約を削除
94
+ button.translatesAutoresizingMaskIntoConstraints = false
95
+ // 大きさを指定
96
+ button.heightAnchor.constraint(equalToConstant: starSize.height).isActive = true
97
+ button.widthAnchor.constraint(equalToConstant: starSize.width).isActive = true
98
+ // ボタンアクションを設定
99
+ button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)
100
+ // Add the button to the stack
101
+ addArrangedSubview(button)
102
+
103
+ // Add the new button to the rating button array
104
+ ratingButtons.append(button)
105
+ }
106
+
107
+ updateButtonSelectionStates()
108
+ }
109
+
110
+ //MARK: Button Action
111
+
112
+ @objc func ratingButtonTapped(button: UIButton) {
113
+ print("Button pressed ????")
114
+
115
+ guard let index = ratingButtons.index(of: button) else {
116
+ fatalError("The button, (button), is not in the ratingButtons array: (ratingButtons)")
117
+ }
118
+
119
+ let selectedRating = index + 1
120
+
121
+ if selectedRating == rating {
122
+ rating = 0
123
+ } else {
124
+ rating = selectedRating
125
+ }
126
+ }
127
+
128
+ private func updateButtonSelectionStates() {
129
+ for (index, button) in ratingButtons.enumerated() {
130
+ button.isSelected = index < rating
131
+ }
132
+ }
133
+ }
134
+
135
+ ```