CABasicAnimation - 视网膜设备上图像的无限滚动是乱码

时间:2013-03-01 02:24:32

标签: objective-c xcode cabasicanimation

在一篇类似的文章中,我找到了无限制动画滚动图像的解决方案:

Animate infinite scrolling of an image in a seamless loop

虽然此解决方案运行良好,但在视网膜设备上运行时似乎存在问题。特别是,我只在iPad上运行这个项目。非视网膜iPad 2可以毫无问题地滚动图像。但是在视网膜iPad 3或4上运行图像是一团糟!这很难描述,但我能说的最好的是它是乱码。像素在每个方向都被拉伸。它类似于Jackson Pollak的画作。

屏幕截图:

http://imgur.com/Q7n08kv

我使用非视网膜图像(非2x)和视网膜版本(@ 2x)进行了测试。图像很大 - 全屏(风景),4面板宽(4096 x 768)。我玩了较小的图像,但结果相同。

CABasicAnimation的滚动功能是否会影响视网膜设备?以下是我正在使用的代码(由rob mayoff提供):

UIImage *crawlImage = [UIImage imageNamed:@"CrawlBackground.png"];
UIColor *crawlPattern = [UIColor colorWithPatternImage:crawlImage];
self.crawlLayer = [CALayer layer];
self.crawlLayer.backgroundColor = crawlPattern.CGColor;

self.crawlLayer.transform = CATransform3DMakeScale(1, -1, 1);
self.crawlLayer.anchorPoint = CGPointMake(0, 1);

self.crawlLayer.frame = CGRectMake(0, 0, crawlImage.size.width + 1024, crawlImage.size.height);

[self.backgroundCrawl.layer addSublayer:self.crawlLayer];
self.backgroundCrawl.layer.zPosition = 0;

CGPoint startPoint = CGPointZero;
CGPoint endPoint = CGPointMake(-crawlImage.size.width, 0);

self.crawlLayerAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
self.crawlLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
self.crawlLayerAnimation.fromValue = [NSValue valueWithCGPoint:startPoint];
self.crawlLayerAnimation.toValue = [NSValue valueWithCGPoint:endPoint];
self.crawlLayerAnimation.repeatCount = HUGE_VALF;
self.crawlLayerAnimation.duration = 30;   // nn seconds to complete the cycle

2 个答案:

答案 0 :(得分:0)

self.crawlLayer.contents = (id)crawlImage.CGImage;

或:

self.crawlLayer.contents = (__bridge id)([crawlImage CGImage]);

这应该解决它。允许大图像正确滚动。

答案 1 :(得分:0)

self.crawlLayer = [CALayer layer];

创建图层后,您应该设置其contentsScale

self.crawlLayer.contentsScale = [[UIScreen mainScreen] scale];

否则在双分辨率屏幕上可能无法正确显示。