2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Image Literal, Color Literalの活用法

Posted at

うまく補完してくれないとき

Color Literalの場合は特にcolor...と途中までタイプした時点で補完機能によって
Color Literalが候補に上がってくれないと非常に使いにくいが
参照が入れ子になっていて、候補に上がってくれない時

before.swift
//imageFromColorは自作関数 //color litral...とタイプしていっても補完してくれないときある
let hogehogeBackImage = UIImage.imageFromColor(color: #colorLiteral(hogehuga))
after.swift
//参照が入れ子でなければうまく補完してくれる
let backColor = #colorLiteral(hugahuga)
//参照するか、右辺だけコピペする
let hogehogeBackImage = UIImage.imageFromColor(color: backColor)

alphaを動的にいじりたいとき

一回Literalにするとパネル表示になるので、パラメータを動的に変化させたいときどうすんの?となる
(そもそもLiteralは見た目をVC側で見ながらやりたいものなので、rgbも変化させたいものには入れたくなくなりがちでは?)
→でもalphaぐらいはいじりたいときある、、、

let backAlpha = hugahuga //ここ計算式にすれば動的に変化させられる
let backColor = #colorLiteral(hugahuga).withAlphaComponent(backAlpha)

参考記事

Xcode 8 の新機能!画像と色のリテラル、画像のコード補完 | DevelopersIO

あとで追記

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?