いつもお世話になります。
StroyBoardで設置したUIButtonに、コード上でグラデーションレイヤーを設定し、
設定したレイヤーをとりのぞいてボタンを初期状態に戻したいのですが、どのようにすればよいでしょうか?
swift
1 2↓これでグラデーションを追加 3 4// ボタン背景にグラデーションを変更する関数 5 func changeSelectedButtonBackground (Button: UIButton) { 6 // 選択されたボタンのグラデーション色を設定する 7 let topColor = UIColor.hex(hexStr: "AAAAAA", alpha: 1.0) 8 let bottomColor = UIColor.hex(hexStr: "686868", alpha: 1.0) 9 //グラデーションの色を配列で管理 10 let gradientColors: [CGColor] = [topColor.cgColor, bottomColor.cgColor] 11 //グラデーションレイヤーを作成 12 let gradientLayer: CAGradientLayer = CAGradientLayer() 13 //グラデーションの色をレイヤーに割り当てる 14 gradientLayer.colors = gradientColors 15 //グラデーションレイヤーを対象ボタンのサイズにする 16 gradientLayer.frame = Button.bounds 17 //グラデーションレイヤーを配置 index(0)に追加しているので、index指定で削除できたら今後自由度高くなるから嬉しい 18 Button.layer.insertSublayer(gradientLayer, at: 0) 19 } 20 21 22↓この関数で特定ボタンのグラデーションを解除したいが、StoryBoardでも背景にグラデーションを掛けてるのでそれもきえてるのかな? 23 24 25 // ボタン背景のグラデーションを取り除く関数 26 func removeGradientFromButtonBackground (Button: UIButton) { 27 Button.layer.sublayers = nil 28 } 29
↓下記を実行した際のプリントログ
swift
1 2// ボタン背景のグラデーションを取り除く関数 3 func changeDefaultButtonBackground (Button: UIButton) { 4 //Button.layer.sublayers?.remove(at: 0) 5 if let layer = Button.layer.sublayers?.first { 6 print("Button.layer.sublayers:",Button.layer.sublayers) 7 layer.removeFromSuperlayer() 8 } 9 }
Button.layer.sublayers:
Optional([<CAGradientLayer:0x168666f0; position = CGPoint (31.75 13.5);
bounds = CGRect (0 0; 63.5 27); allowsGroupOpacity = YES; zPosition = -100;
cornerRadius = 1; colors = (
"<CGColor 0x155c9e90> [<CGColorSpace 0x1565cdb0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0.121569 0.129412 0.141176 1 )",
"<CGColor 0x15591600> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 0.407819 1 )")>,
<CAGradientLayer:0x156755f0; position = CGPoint (31.75 13.25); bounds = CGRect (0 0; 63.5 26.5); allowsGroupOpacity = YES; colors = (
"<CGColor 0x156f9400> [<CGColorSpace 0x1565cdb0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0.666667 0.666667 0.666667 1 )",
"<CGColor 0x15665b00> [<CGColorSpace 0x1565cdb0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0.407843 0.407843 0.407843 1 )")>,
<CALayer:0x1683fd20; position = CGPoint (0 0); bounds = CGRect (0 0; 0 0); delegate = <UIImageView: 0x155294f0; frame = (0 0; 0 0);
clipsToBounds = YES; alpha = 0.2; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1683fd20>>; hidden = YES; masksToBounds = YES; allowsGroupOpacity = YES; opacity = 0.2>,
<_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;
userInteractionEnabled = NO;
layer = <_UILabelLayer: 0x155aa940>>; contents = <CABackingStore 0x15789090 (buffer [123 23] BGRA8888)>; allowsGroupOpacity = YES; opacity = 0.2; contentsMultiplyColor = (null); rasterizationScale = 2; contentsScale = 2>,
<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;
backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
<CALayer:0x16840460; position = CGPoint (59.5 22.5); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x155296d0; frame = (58.5 21.5; 2 2);
layer = <CALayer: 0x16840460>>;
allowsGroupOpacity = YES;
backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
<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;
backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
<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>>;
allowsGroupOpacity = YES; backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
<CALayer:0x156662e0; position = CGPoint (59.5 22.5);bounds = CGRect (0 0; 2 2);delegate = <UIView: 0x15666200;frame = (58.5 21.5; 2 2);
layer = <CALayer: 0x156662e0>>; allowsGroupOpacity = YES;
backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
<CALayer:0x157da490; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x157da3b0; frame = (58.5 22; 2 2);
layer = <CALayer: 0x157da490>>; allowsGroupOpacity = YES;
backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
<CALayer:0x157daa10; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x157da930; frame = (58.5 22; 2 2);
layer = <CALayer: 0x157daa10>>; allowsGroupOpacity = YES;
backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
<CALayer:0x1564b150; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x1565fc20; frame = (58.5 22; 2 2);
layer = <CALayer: 0x1564b150>>; allowsGroupOpacity = YES;
backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
<CALayer:0x16868830; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x15531c30; frame = (58.5 22; 2 2);
layer = <CALayer: 0x16868830>>; allowsGroupOpacity = YES;
backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>,
<CALayer:0x15547290; position = CGPoint (59.5 23); bounds = CGRect (0 0; 2 2); delegate = <UIView: 0x155471b0; frame = (58.5 22; 2 2);
layer = <CALayer: 0x15547290>>; allowsGroupOpacity = YES;
backgroundColor = <CGColor 0x156af0f0> [<CGColorSpace 0x15560350> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 1 1 )>])
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/03/13 07:49
2018/03/13 07:54
退会済みユーザー
2018/03/13 07:59
2018/03/13 08:11
退会済みユーザー
2018/03/13 08:45 編集
2018/03/13 22:32 編集
退会済みユーザー
2018/03/13 09:06 編集
2018/03/13 09:18
退会済みユーザー
2018/03/13 09:39
2018/03/13 10:11 編集
退会済みユーザー
2018/03/13 10:48