clearsContextBeforeDrawing

これはdrawRect実行前に領域を塗り潰すかどうかのパラメータですが、調べれば調べるほど他にもいろいろな設定項目が出てくる、代表的な芋づるパラメータです。
他に出てくるパラーメタはopaque(不透明)とbackgroundColorの2つで、 clearsContextBeforeDrawingが2通り opaqueが2通り backgroundColorがnil、transparent(alphaが1未満)、opaque(alphaが1)の3通りありますので 合計12パターンの設定があります。
ややこしいですが、各パラメータの説明のまえに王道をいうと 何かの色で塗りつぶすなら、backgroundColorにベース色を指定します。 透明にしたいならview.backgroundColor = [UIColor clearColor]に指定します。 ただしパフォーマンスを上げたい場合や、仕組みが気になる場合は、各パラメータの意味を理解して対応することになります。 drawRectメソッドの内で領域をすべて塗る場合には効果があります。[UIColor clearColor]は色値とアルファ値が全部0.0のUIColorインスタンス。いわゆる透明な黒というやつである。

詳しく見ていくと
backgroundColorが指定され、alphaが1.0の時
その色で塗る
opaqueとclearsContextBeforeDrawingの設定による違いはない その色で塗られているので下の画像が塗られているかと前もって領域が透明な黒でクリアされているかは見た目ではわからないがリファレンスによるとクリアされているようだ。

backgroundColorが指定され、alphaが0.0<=alpha<1.0の時
その色を塗りながら下の画像を表示
opaqueとclearsContextBeforeDrawingの設定による違いはない 下の画像が塗られているのでopaqueの設定が無視されるようだ 前もって領域が透明な黒でクリアされているかは見た目からはわからないがこれもリファレンスによるとクリアされているようだ。
上で出した[UIColor clearColor]の場合、全部0.0であろうと指定されているということで、その色(というかデータ)で塗るようである。

backgroundColorがnilのとき
opaqueがNOのときは下の画像を描く opaqueがYESのときは下の画像を描かない
clearsContextBeforeDrawingがYESの時は透明黒で塗りつぶし? clearsContextBeforeDrawingがNOの時は前回データが残ったまま? という単純な処理をするようです。
backgroundColorがnilというはリファレンスによると透明と描いてあるが上の処理からもその説明は妥当だ。

なんでややこしいのか。iOSが実装されてきた歴史なども関係あるんでしょう。

ちなみに公式の英語の説明を乗せておきます
Discussion When set to YES, the drawing buffer is automatically cleared to transparent black before the drawRect: method is called. This behavior ensures that there are no visual artifacts left over when the view’s contents are redrawn. If the view’s opaque property is also set to YES, the backgroundColor property of the view must not be nil or drawing errors may occur. The default value of this property is YES. If you set the value of this property to NO, you are responsible for ensuring the contents of the view are drawn properly in your drawRect: method. If your drawing code is already heavily optimized, setting this property is NO can improve performance, especially during scrolling when only a portion of the view might need to be redrawn.

コメント

このブログの人気の投稿

Swiftのコンパイルエラー寄せ集め

コンパイルエラー覚え書き(Objective-C)

AVAudioSession細かいことまとめ(late 2014)