質問編集履歴

4

修正

2018/03/13 08:35

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -6,128 +6,212 @@
6
6
 
7
7
  設定したレイヤーをとりのぞいてボタンを初期状態に戻したいのですが、どのようにすればよいでしょうか?
8
8
 
9
+
10
+
9
11
  ```swift
10
12
 
13
+
14
+
15
+ ↓これでグラデーションを追加
16
+
17
+
18
+
19
+ // ボタン背景にグラデーションを変更する関数
20
+
21
+ func changeSelectedButtonBackground (Button: UIButton) {
22
+
23
+ // 選択されたボタンのグラデーション色を設定する
24
+
25
+ let topColor = UIColor.hex(hexStr: "AAAAAA", alpha: 1.0)
26
+
27
+ let bottomColor = UIColor.hex(hexStr: "686868", alpha: 1.0)
28
+
29
+ //グラデーションの色を配列で管理
30
+
31
+ let gradientColors: [CGColor] = [topColor.cgColor, bottomColor.cgColor]
32
+
11
- //グラデーションレイヤーをボタンに配置
33
+ //グラデーションレイヤーを作成
34
+
12
-
35
+ let gradientLayer: CAGradientLayer = CAGradientLayer()
36
+
37
+ //グラデーションの色をレイヤーに割り当てる
38
+
39
+ gradientLayer.colors = gradientColors
40
+
41
+ //グラデーションレイヤーを対象ボタンのサイズにする
42
+
43
+ gradientLayer.frame = Button.bounds
44
+
45
+ //グラデーションレイヤーを配置 index(0)に追加しているので、index指定で削除できたら今後自由度高くなるから嬉しい
46
+
13
- XXXButton.layer.insertSublayer(gradientLayer, at: 0)
47
+ Button.layer.insertSublayer(gradientLayer, at: 0)
48
+
49
+ }
50
+
51
+
52
+
53
+
54
+
55
+ ↓この関数で特定ボタンのグラデーションを解除したいが、StoryBoardでも背景にグラデーションを掛けてるのでそれもきえてるのかな?
56
+
57
+
58
+
59
+
60
+
61
+ // ボタン背景のグラデーションを取り除く関数
62
+
63
+ func removeGradientFromButtonBackground (Button: UIButton) {
64
+
65
+ Button.layer.sublayers = nil
66
+
67
+ }
68
+
69
+
14
70
 
15
71
  ```
16
72
 
17
73
 
18
74
 
19
- ↓のようにやてみたらボタがまるごと消えました
75
+ 下記を実行した際プリトログ
20
76
 
21
77
 
22
78
 
23
79
  ```swift
24
80
 
81
+
82
+
83
+ // ボタン背景のグラデーションを取り除く関数
84
+
85
+ func changeDefaultButtonBackground (Button: UIButton) {
86
+
87
+ //Button.layer.sublayers?.remove(at: 0)
88
+
89
+ if let layer = Button.layer.sublayers?.first {
90
+
91
+ print("Button.layer.sublayers:",Button.layer.sublayers)
92
+
25
- XXXButton.layer.removeFromSuperlayer()
93
+ layer.removeFromSuperlayer()
94
+
95
+ }
96
+
97
+ }
26
98
 
27
99
  ```
28
100
 
29
101
 
30
102
 
31
- インデックスを指定していして特定レイヤーだけを削除することはできるのでしょうか?
32
-
33
- それとも他の方法がよいのでしょうか?
34
-
35
-
36
-
37
- ↓こちらの方法が正解っぽいらしいが、
38
-
39
- ボタンの文字列が消える。レイヤー番号か名称の指定方法がわからない。
40
-
41
- ```swift
42
-
43
- XXXButton.layer.sublayers = nil
44
-
45
- ```
46
-
47
-
48
-
49
- 回答ありがとうございます。
50
-
51
- 質問が雑だと思うので、詳しく追記します。
52
-
53
-
54
-
55
- ```swift
56
-
57
-
58
-
59
- ↓これでグラデーションを追加
60
-
61
-
62
-
63
- // ボタン背景にグラデーションを変更する関数
64
-
65
- func changeSelectedButtonBackground (Button: UIButton) {
66
-
67
- // 選択されたボタンのグラデーション色を設定する
68
-
69
- let topColor = UIColor.hex(hexStr: "AAAAAA", alpha: 1.0)
70
-
71
- let bottomColor = UIColor.hex(hexStr: "686868", alpha: 1.0)
72
-
73
- //グラデーションの色を配列で管理
74
-
75
- let gradientColors: [CGColor] = [topColor.cgColor, bottomColor.cgColor]
76
-
77
- //グラデーションレイヤーを作成
78
-
79
- let gradientLayer: CAGradientLayer = CAGradientLayer()
80
-
81
- //グラデーションの色をレイヤーに割り当てる
82
-
83
- gradientLayer.colors = gradientColors
84
-
85
- //グラデーションレイヤーを対象ボタンのサイズにする
86
-
87
- gradientLayer.frame = Button.bounds
88
-
89
- //グラデーションレイヤーを配置 index(0)に追加しているので、index指定で削除できたら今後自由度高くなるから嬉しい
90
-
91
- Button.layer.insertSublayer(gradientLayer, at: 0)
92
-
93
- }
94
-
95
-
96
-
97
-
98
-
99
- ↓この関数で特定ボタンのグラデーションを解除したいが、StoryBoardでも背景にグラデーションを掛けてるのでそれもきえてるのかな?
100
-
101
-
102
-
103
-
104
-
105
- // ボタン背景のグラデーションを取り除く関数
106
-
107
- func removeGradientFromButtonBackground (Button: UIButton) {
108
-
109
- Button.layer.sublayers = nil
110
-
111
- }
112
-
113
-
114
-
115
- ```
116
-
117
- グラデ追加ボタンタップでグラデ追加可能
118
-
119
-
120
-
121
- グラデ解除ボタンタップでデフォルトグラデーション・設定したグラデーション・ボタンタイトルが消える
122
-
123
-
124
-
125
- グラデ追加ボタンタップでランタイムエラー
126
-
127
-
128
-
129
- 2018-03-13 16:20:58.935062 testPG[6188:3707982] -[_TtGCs23_ContiguousArrayStorageP__ isHidden]: unrecognized selector sent to instance 0x16018030
130
-
131
- 2018-03-13 16:20:58.936064 testPG[6188:3707982] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtGCs23_ContiguousArrayStorageP__ isHidden]: unrecognized selector sent to instance 0x16018030'
132
-
133
- *** First throw call stack:
103
+
104
+
105
+ Button.layer.sublayers:
106
+
107
+
108
+
109
+ Optional([<CAGradientLayer:0x168666f0; position = CGPoint (31.75 13.5);
110
+
111
+ bounds = CGRect (0 0; 63.5 27); allowsGroupOpacity = YES; zPosition = -100;
112
+
113
+ cornerRadius = 1; colors = (
114
+
115
+ "<CGColor 0x155c9e90> [<CGColorSpace 0x1565cdb0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0.121569 0.129412 0.141176 1 )",
116
+
117
+ "<CGColor 0x15591600> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 0.407819 1 )")>,
118
+
119
+
120
+
121
+ <CAGradientLayer:0x156755f0; position = CGPoint (31.75 13.25); bounds = CGRect (0 0; 63.5 26.5); allowsGroupOpacity = YES; colors = (
122
+
123
+ "<CGColor 0x156f9400> [<CGColorSpace 0x1565cdb0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0.666667 0.666667 0.666667 1 )",
124
+
125
+ "<CGColor 0x15665b00> [<CGColorSpace 0x1565cdb0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0.407843 0.407843 0.407843 1 )")>,
126
+
127
+
128
+
129
+ <CALayer:0x1683fd20; position = CGPoint (0 0); bounds = CGRect (0 0; 0 0); delegate = <UIImageView: 0x155294f0; frame = (0 0; 0 0);
130
+
131
+ clipsToBounds = YES; alpha = 0.2; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1683fd20>>; hidden = YES; masksToBounds = YES; allowsGroupOpacity = YES; opacity = 0.2>,
132
+
133
+
134
+
135
+ <_UILabelLayer:0x155aa940; position = CGPoint (31.75 13.75); bounds = CGRect (0 0; 51.5 11.5); delegate = <UIButtonLabel: 0x155a0ee0; frame = (6 8; 51.5 11.5); text = 'CLIPLINES'; alpha = 0.2; opaque = NO;
136
+
137
+ userInteractionEnabled = NO;
138
+
139
+
140
+
141
+ layer = <_UILabelLayer: 0x155aa940>>; contents = <CABackingStore 0x15789090 (buffer [123 23] BGRA8888)>; allowsGroupOpacity = YES; opacity = 0.2; contentsMultiplyColor = (null); rasterizationScale = 2; contentsScale = 2>,
142
+
143
+
144
+
145
+ <CALayer:0x1683d990; position = CGPoint (59.5 22.5);bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x155295f0; frame = (58.5 21.5; 2 2); layer = <CALayer: 0x1683d990>>; allowsGroupOpacity = YES;
146
+
147
+ backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
148
+
149
+
150
+
151
+ <CALayer:0x16840460; position = CGPoint (59.5 22.5); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x155296d0; frame = (58.5 21.5; 2 2);
152
+
153
+ layer = <CALayer: 0x16840460>>;
154
+
155
+ allowsGroupOpacity = YES;
156
+
157
+ backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
158
+
159
+
160
+
161
+ <CALayer:0x1552d0b0; position = CGPoint (59.5 22.5); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x1552cfd0; frame = (58.5 21.5; 2 2); layer = <CALayer: 0x1552d0b0>>; allowsGroupOpacity = YES;
162
+
163
+ backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
164
+
165
+
166
+
167
+ <CALayer:0x1561e380; position = CGPoint (59.5 22.5); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x15651c50; frame = (58.5 21.5; 2 2); layer = <CALayer: 0x1561e380>>;
168
+
169
+ allowsGroupOpacity = YES; backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
170
+
171
+
172
+
173
+ <CALayer:0x156662e0; position = CGPoint (59.5 22.5);bounds = CGRect (0 0; 2 2);delegate = <UIView: 0x15666200;frame = (58.5 21.5; 2 2);
174
+
175
+ layer = <CALayer: 0x156662e0>>; allowsGroupOpacity = YES;
176
+
177
+ backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
178
+
179
+
180
+
181
+ <CALayer:0x157da490; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x157da3b0; frame = (58.5 22; 2 2);
182
+
183
+ layer = <CALayer: 0x157da490>>; allowsGroupOpacity = YES;
184
+
185
+ backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
186
+
187
+
188
+
189
+ <CALayer:0x157daa10; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x157da930; frame = (58.5 22; 2 2);
190
+
191
+ layer = <CALayer: 0x157daa10>>; allowsGroupOpacity = YES;
192
+
193
+ backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
194
+
195
+
196
+
197
+ <CALayer:0x1564b150; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x1565fc20; frame = (58.5 22; 2 2);
198
+
199
+ layer = <CALayer: 0x1564b150>>; allowsGroupOpacity = YES;
200
+
201
+ backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
202
+
203
+
204
+
205
+ <CALayer:0x16868830; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x15531c30; frame = (58.5 22; 2 2);
206
+
207
+ layer = <CALayer: 0x16868830>>; allowsGroupOpacity = YES;
208
+
209
+ backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
210
+
211
+
212
+
213
+ <CALayer:0x15547290; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x155471b0; frame = (58.5 22; 2 2);
214
+
215
+ layer = <CALayer: 0x15547290>>; allowsGroupOpacity = YES;
216
+
217
+ backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>])

3

追記

2018/03/13 08:35

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,91 @@
43
43
  XXXButton.layer.sublayers = nil
44
44
 
45
45
  ```
46
+
47
+
48
+
49
+ 回答ありがとうございます。
50
+
51
+ 質問が雑だと思うので、詳しく追記します。
52
+
53
+
54
+
55
+ ```swift
56
+
57
+
58
+
59
+ ↓これでグラデーションを追加
60
+
61
+
62
+
63
+ // ボタン背景にグラデーションを変更する関数
64
+
65
+ func changeSelectedButtonBackground (Button: UIButton) {
66
+
67
+ // 選択されたボタンのグラデーション色を設定する
68
+
69
+ let topColor = UIColor.hex(hexStr: "AAAAAA", alpha: 1.0)
70
+
71
+ let bottomColor = UIColor.hex(hexStr: "686868", alpha: 1.0)
72
+
73
+ //グラデーションの色を配列で管理
74
+
75
+ let gradientColors: [CGColor] = [topColor.cgColor, bottomColor.cgColor]
76
+
77
+ //グラデーションレイヤーを作成
78
+
79
+ let gradientLayer: CAGradientLayer = CAGradientLayer()
80
+
81
+ //グラデーションの色をレイヤーに割り当てる
82
+
83
+ gradientLayer.colors = gradientColors
84
+
85
+ //グラデーションレイヤーを対象ボタンのサイズにする
86
+
87
+ gradientLayer.frame = Button.bounds
88
+
89
+ //グラデーションレイヤーを配置 index(0)に追加しているので、index指定で削除できたら今後自由度高くなるから嬉しい
90
+
91
+ Button.layer.insertSublayer(gradientLayer, at: 0)
92
+
93
+ }
94
+
95
+
96
+
97
+
98
+
99
+ ↓この関数で特定ボタンのグラデーションを解除したいが、StoryBoardでも背景にグラデーションを掛けてるのでそれもきえてるのかな?
100
+
101
+
102
+
103
+
104
+
105
+ // ボタン背景のグラデーションを取り除く関数
106
+
107
+ func removeGradientFromButtonBackground (Button: UIButton) {
108
+
109
+ Button.layer.sublayers = nil
110
+
111
+ }
112
+
113
+
114
+
115
+ ```
116
+
117
+ グラデ追加ボタンタップでグラデ追加可能
118
+
119
+
120
+
121
+ グラデ解除ボタンタップでデフォルトグラデーション・設定したグラデーション・ボタンタイトルが消える
122
+
123
+
124
+
125
+ グラデ追加ボタンタップでランタイムエラー
126
+
127
+
128
+
129
+ 2018-03-13 16:20:58.935062 testPG[6188:3707982] -[_TtGCs23_ContiguousArrayStorageP__ isHidden]: unrecognized selector sent to instance 0x16018030
130
+
131
+ 2018-03-13 16:20:58.936064 testPG[6188:3707982] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtGCs23_ContiguousArrayStorageP__ isHidden]: unrecognized selector sent to instance 0x16018030'
132
+
133
+ *** First throw call stack:

2

誤記修正

2018/03/13 07:32

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -40,6 +40,6 @@
40
40
 
41
41
  ```swift
42
42
 
43
- Button.layer.sublayers = nil
43
+ XXXButton.layer.sublayers = nil
44
44
 
45
45
  ```

1

変更

2018/03/13 07:02

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -31,3 +31,15 @@
31
31
  インデックスを指定していして特定レイヤーだけを削除することはできるのでしょうか?
32
32
 
33
33
  それとも他の方法がよいのでしょうか?
34
+
35
+
36
+
37
+ ↓こちらの方法が正解っぽいらしいが、
38
+
39
+ ボタンの文字列が消える。レイヤー番号か名称の指定方法がわからない。
40
+
41
+ ```swift
42
+
43
+ Button.layer.sublayers = nil
44
+
45
+ ```