改变图案图像的相位 [英] Changing the phase of a pattern image

查看:127
本文介绍了改变图案图像的相位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用图案图像作为 UITableViewCell 的背景色,

iI'm using a pattern image as a background color for a UITableViewCell by

cell.backgroundColor = [UIColor colorWithPatternImage[UIImage imageNamed:@"image.png"]]

但是,我希望图案图像的底部与单元格的底部对齐。默认情况下,图像的左上角与单元格的原点对齐。我需要这个的原因是因为单元格的高度是可变的,但是无论高度如何,底部都需要显示相同。

However, I would like the bottom of the pattern image to align with the bottom of the cell. By default, the top left corner of the image is aligned with the origin of the cell. The reason I need this is because the cell's height is variable, but the bottom needs to appear the same regardless of the height.

Apple文档指定了更改在图案图像的阶段,应将颜色( [UIColor colorWothPatternImage] )设置为当前颜色,然后调用CGContextSetPatternPhase。但是,我不确定如何在 cellForRowAtIndexPath 中设置 UITableViewCell 的backgroundColor的情况下使用此方法。

The Apple docs specify that in order to change the phase of a pattern image, the color ([UIColor colorWothPatternImage]) should be made the current color, and then CGContextSetPatternPhase should be called. However, I am unsure of how to use this method in the context of setting the UITableViewCell's backgroundColor in cellForRowAtIndexPath.

我意识到之前已经有人问过这个问题,但是它并没有回答我的问题。

I realize that this has been asked before, however it did not answer my question.

推荐答案

我看到这个问题是很久以前提出的,您可能已经找到了解决方案,但是我偶然发现了一个相同的问题并找到了解决方案,并认为应该将其发布以备将来参考。 。没有一个以上的人对同一件事感到头疼:)

I see that this question was asked a long time ago, and you probably have found a solution to it already, but I stumbled into the same problem and found a solution and figured I should post it for future reference. No sense in more than one person having a headache over the same thing :)

首先,您必须继承要移入UIColor的任何视图。情况, UITableViewCell

First you have to subclass whatever view you want the UIColor to be shifted in. So in this case, UITableViewCell.

。最后,在 drawRect中使用此代码:方法移动原点。

..Lastly, use this code in the drawRect: method to shift the origin. And you're done!

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGSize phase = CGSizeMake(0, self.bounds.size.height); // CHANGE THESE VALUES TO WHATEVER X/Y SHIFT YOU NEED
CGContextSetPatternPhase(context, phase);

CGColorRef color = self.backgroundColor.CGColor;
CGContextSetFillColorWithColor(context, color);
CGContextFillRect(context, self.bounds);

CGContextRestoreGState(context);

通过将原点的宽度和高度值替换为原点,即可进行移位。

The shift works by displacing the origin by the width and height values of 'phase'.

希望有帮助! (不要忘记 [super drawRect:] !)

Hope that helps! (Don't forget [super drawRect:]!)

这篇关于改变图案图像的相位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆