2010-04-06 24 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