0

backgroundプロパティは、通常のUIViewのIBのアウトレットです。

UIImage* image = [UIImage imageNamed:@"glow.jpg"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 768), NO, 0.0);
[image drawInRect:CGRectMake(0, 0, 1, 768)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
self.background.backgroundColor = [UIColor colorWithPatternImage:newImage];

self.backgroundにカスタムクラスを追加しない場合、ビューが自分自身のサイズを変更してもグローはそれ自体のサイズを変更せず(グラデーションです)、768の高さで繰り返されます。

ただし、UIViewをサブクラス化し、backgroundプロパティをそのクラスに設定し、drawRectメソッドをオーバーライドすると、パターン画像のサイズがビューの高さになります。

非常に奇妙な、誰かがこれを説明できるかどうか疑問に思いますか?

4

1 に答える 1

1

iOS:UIViewの「drawRect:」とレイヤーの遅延「drawLayer:inContext:」の使用

-(void)drawRect:(CGRect)r
{

    // UIView uses the existence of -drawRect: to determine if it should allow its CALayer
    // to be invalidated, which would then lead to the layer creating a backing store and
    // -drawLayer:inContext: being called.
    // By implementing an empty -drawRect: method, we allow UIKit to continue to implement
    // this logic, while doing our real drawing work inside of -drawLayer:inContext:

}



-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
    ...
}
于 2013-08-13T08:47:26.780 に答える