UIScrollView与图案图像作为背景

时间:2012-05-16 14:04:13

标签: ipad background uiscrollview

这是我面临的问题:我正在制作一个 UIScrollView ,它将有一个模式图像来填充背景。也就是说,我需要一个背景来滚动UIScrollView。一个很好的例子是iPad上的 游戏中心 应用程序。使用scrollview,背景将平滑滚动。

目前我有两种方法可以实现这种效果,但它们都没有提供良好的性能。 首先我尝试了这个

self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:kRLCastViewBGUnit]];

但不幸的是,滚动性能非常糟糕并占用了太多内存。

然后我尝试在drawRect中使用CGContextDrawTiledImage,如下所示:

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

    CGContextTranslateCTM(currentContext, 0, rect.size.height);
    CGContextScaleCTM(currentContext, 1.0, -1.0);

    CGContextClipToRect(currentContext, rect);
    CGRect centerTileRect = CenterTileRect;
    CGContextDrawTiledImage(currentContext, centerTileRect, [ResourceProvider backgroundTileImageRef]);

}

在新iPad上仍然不尽如人意。我一定做错了什么或误用了一些方法,因为Game Center在滚动时表现得很好。任何人都可以为我提供解决UIScrollView背景性能问题的解决方案吗?非常感谢!!

1 个答案:

答案 0 :(得分:0)

您应该使用UITableView。在这里你可以轻松设置cell.backgroundView。

UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake:(0.0,0.0,320.0, height)];
backgroundView.image = [UIImage imageNamed:@"Shelf.jpeg"];
cell.backgroundView = backgroundView;

如果你真的只想使用UIScrollView,那么只需要一个单元格背景图像,图像宽度应该与scrollView的宽度相同,因为colorWithPatternImage:方法就像tile属性一样。

只将此图片带入您的项目并将其用作UIScrollView的backgroundColor。

[scrollView setContentSize:CGSizeMake(320.0, 1000.0)];
[scrollView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"cellImage.png"]]];
相关问题