这是我面临的问题:我正在制作一个 UIScrollView,它将有一个图案图像来填充背景。也就是说,我需要一个背景来与 UIScrollView 一起滚动。 iPad 上的Game Center 应用就是一个很好的例子。背景将随着 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 背景性能问题的解决方案吗?非常感谢!!
最佳答案
您应该改用 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: 方法像平铺属性一样工作。
仅将此图像放入您的项目中并将其用作 UIScrollView 的背景颜色。
[scrollView setContentSize:CGSizeMake(320.0, 1000.0)];
[scrollView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"cellImage.png"]]];
关于ipad - 以图案图像为背景的 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10620248/