gpt4 book ai didi

iphone - 使用 [UIColor colorWithPatternImage :]? 时如何更改图案相位

转载 作者:搜寻专家 更新时间:2023-10-30 20:22:32 24 4
gpt4 key购买 nike

我正在使用 [UIColor colorWithPatternImage:] 为我的分组 UITableViewCell 设置背景图像。我的单元格有免费的圆角,一切看起来都很棒,直到我尝试给单元格一个不同的高度。

理想情况下,我希望背景图像垂直居中,但 colorWithPatternImage: 方法从左下角开始图像图案,这让我的单元格看起来很糟糕。

文档中说

"默认情况下,返回颜色的相位为0,这会导致图像的左上角与绘图原点对齐。要更改相位,请将颜色设置为当前颜色,然后使用 CGContextSetPatternPhase 函数更改相位。”

但我不知道如何在我当前的实现中使用 CGContextSetPatternPhase

我试过了

- (void)drawRect:(CGRect)rect 
{
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context);

CGSize phase = CGSizeMake(50, 50);
CGContextSetPatternPhase(context, phase);

UIImage* image = [UIImage imageNamed:@"cell_background"];

UIColor* color = [UIColor colorWithPatternImage:image];

self.backgroundColor = color;

CGContextRestoreGState(context);

[super drawRect:rect];
}

但这对模式阶段没有任何影响。谁能告诉我应该如何使用它?

非常感谢,

约瑟夫

最佳答案

Core Graphics 调用通常用作特定绘图过程的一部分;设置 backgroundColor 属性实际上并没有绘制任何东西,它只是设置 View 在默认情况下如何绘制自己。 Quartz 2D 编程指南有一个 explanation of how to draw using pattern images ;你也许可以走一条捷径

CGColorRef color = [UIColor colorWithPatternImage:image].CGColor;
CGContextSetFillColorWithColor(context, color);
CGContextFillRect(context, self.bounds);

...但我不能保证,因为 UIColor 模式可能无法与 Core Graphics 很好地配合。您可能必须采用上面链接中描述的更详细的路线。

关于iphone - 使用 [UIColor colorWithPatternImage :]? 时如何更改图案相位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6604063/

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