gpt4 book ai didi

ios - 当我子类化 UIView 并实现空的 drawRect 方法时, colorWithPatternImage 图像会调整大小以填充 View ,为什么?

转载 作者:行者123 更新时间:2023-11-29 04:24:31 24 4
gpt4 key购买 nike

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 添​​加自定义类,当 View 调整自身大小时,发光不会调整自身大小(它是渐变),它会在 768 高度重复。

但是,如果我对 UIView 进行子类化,将背景属性设置为该类并重写 drawRect 方法,它会将图案图像的大小调整为 View 的高度。

很奇怪,不知道有没有人能解释一下?

最佳答案

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
{
...
}

关于ios - 当我子类化 UIView 并实现空的 drawRect 方法时, colorWithPatternImage 图像会调整大小以填充 View ,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12535497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com