2010-04-06 26 views
2

在iPhone上,我怎么能达到相同的平铺背景效果?如何在网页上制作平铺背景?

例如,我有一个模式图像,我想只重复水平。我必须在-drawRect:我自己的路线,并循环绘制模式,或有一个方便的方法来做到这一点?

回答

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

    UIImage* img = [UIImage imageNamed:@"transparency.png"]; 
    CGContextDrawTiledImage(currentContext, CGRectMake(0, 0, 12, 12), [img CGImage]); 
} 

在此示例中,“transparency.png”是12x12图像。

6

您可以设置的UIView的backgroundColor使用下面的代码段的模式:

UIImage *image = [UIImage imageNamed:@"pattern.png"]; 
myView.backgroundColor = [UIColor colorWithPatternImage:image]; 

的图像将自动被平铺为您覆盖的地区。

克劳斯

+0

谢谢克劳斯,这很好。但不幸的是图像中的透明度随着这个变成了黑色。 – dontWatchMyProfile 2010-04-06 15:01:57

+0

视图是最后面的视图,还是你有一个不透明的视图背后? – 2010-04-06 15:27:02

+0

我更喜欢你的解决方案,克劳斯。谢谢:) – Emil 2010-04-06 16:14:42