むい空間
むいむい(´ω`*)
Entries
@interface MyView : UIView
{
int n;
}
@end
@implementation MyView
- (void)drawRect:(CGRect)rect {
n = (n + 1) * 10;
[[UIColor whiteColor] set];
NSString *string = [NSString stringWithFormat:@"%d", n];
[string drawInRect:rect withFont:[UIFont systemFontOfSize:20]];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self setNeedsDisplay];
}
@end
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(10, 10, 400, 200)];
assert(myView.clearsContextBeforeDrawing); // 初期値の確認
assert(myView.opaque); // 初期値は「不透明」のはず
[viewController.view addSubview:myView];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
タップされる度に「1桁目が0でそれ以外が1」の数字を桁を増やしながら描くビューを追加するコード。
9回ほどタップするとオーバーフローするがまあそれはさておき。
これをアクティブSDK「Simulator 2.2.1」でビルドするとこうなるが、
アクティブSDK「Simulator 3.0」でビルドするとこうなる。
2.2.1では毎回黒塗りされるからてっきり backgroundColor が nil ならopaqueなビューの背景は「不透明な黒([UIColor blackColor])」になるかと思ったがそんなことはなかった。
opaque を NO にするか、 backgroundColor になんらかの色を設定すれば、3.0でも綺麗にクリアしてくれるようになる。
opaqueなビューは、 ビュー全体を不透明色で塗りつぶさなければどうなるか分からない
ので、 drawRect: で完全に塗りつぶされないなら backgroundColor の設定は必須。
The results are unpredictable if opaque and the view doesn’t fill its bounds.
clearsContextBeforeDrawing を YES にしても、 drawRect: の前にグラフィックコンテキストが「透明な黒(
だけなので、ビューを不透明に塗りつぶす手助けにはならない。[UIColor clearColor])」でクリアされるようになる
If YES, the current graphics context buffer in the drawRect: method is automatically cleared to transparent black before drawRect: is invoked.
コメント
コメントの投稿
トラックバック
- トラックバック URL
- http://idlysphere.blog66.fc2.com/tb.php/206-bd7020bc
- この記事にトラックバックする(FC2ブログユーザー)