0

background 属性是 IB 中常规 UIView 的一个出口。

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 子类化,将背景属性设置为该类并覆盖 drawRect 方法,它会将图案图像的大小调整为视图的高度。

很奇怪,请问有大神能解释一下吗?

4

1 回答 1

1

iOS:使用 UIView 的 'drawRect:' 与它的图层的 delagate '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 回答