gpt4 book ai didi

iphone - Core Animation CALayer mask 动画表现

转载 作者:IT王子 更新时间:2023-10-29 08:06:10 26 4
gpt4 key购买 nike

我们想在我们的 iPhone 应用程序中使用 UITabBar,但有一个异常(exception):我们有一个“同步”按钮,我想在同步操作发生时旋转它。

alt text

不幸的是,这意味着必须创建一个自定义标签栏,但这既不是这里也不是那里:我使用 Core Animation 实现的动画看起来很棒。问题是,在设置动画时,它会对屏幕上使用动画的其他所有内容的性能产生不利影响:UITableView 滚动、MKMapView 平移和引脚掉落等。我的测试设备是 iPhone 4。

问题似乎是我如何实现标签栏 - 我想实现与 UITabBar 非常相似的东西,你只需为图标提供 PNG,它使用 alpha channel 创建正常和突出显示状态掩盖背景图像。我用 CALayer 的 mask 属性完成了这个:

// Inside a UIView subclass' init method...

// Create the mask layer by settings its contents as our PNG icon.
CALayer *maskLayer = [CALayer layer];
maskLayer.frame = CGRectMake(0, 0, 31, 31);
maskLayer.contentsGravity = kCAGravityCenter;
maskLayer.contentsScale = [[UIScreen mainScreen] scale];
maskLayer.rasterizationScale = [[UIScreen mainScreen] scale];
maskLayer.contents = (id)symbolImage.CGImage;
maskLayer.shouldRasterize = YES;
maskLayer.opaque = YES;

fgLayer = [[CALayer layer] retain];
fgLayer.frame = self.layer.frame;
fgLayer.backgroundColor = [UIColor colorWithImageNamed:@"tabbar-normal-bg.png"].CGColor;
fgLayer.mask = maskLayer; // Apply the mask
fgLayer.shouldRasterize = YES;
fgLayer.opaque = YES;

[self.layer addSublayer:fgLayer];

(注意:在上面的屏幕截图中,您可以看到我还添加了一个阴影层,但为了简单起见,我从代码中删除了它。我在同步图标设置动画时从同步图标中删除了阴影层,所以它不应该不相关。)

要制作动画,我只需旋转 mask 层:

- (void)startAnimating {
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath: @"transform"];
CATransform3D transform = CATransform3DMakeRotation(RADIANS(179.9), 0.0, 0.0, 1.0);
animation.toValue = [NSValue valueWithCATransform3D:transform];
animation.duration = 5;
animation.repeatCount = 10000;
animation.removedOnCompletion = YES;
[fgLayer.mask addAnimation:animation forKey:@"rotate"]; // Add animation to the mask
}

所以这一切都很好,除了性能。你可以看到我已经尝试过在 Google 上出现的关于栅格化图层/使它们不透明的提示 - 没有帮助。

我想我已经确定 mask 层是罪魁祸首。当我取出 mask 层并旋转 fgLayer 而不是它的 mask 时,性能非常好,尽管这肯定不是我想要的效果:

alt text

如果在应用 mask 时旋转 fgLayer 而不是 mask ,性能也和以前一样差。

因此,如果必须重新合成 mask ,动画的每一帧都会变慢,我是否可以使用任何其他技术来实现类似的效果,从而获得更好的性能?使用路径而不是图像作为 mask 层?或者我是否必须降低到 OpenGL 或其他东西以获得良好的性能?

更新:进一步强化了掩码是减速的想法,我的同事建议尝试旋转一个 CALayer,仅将图像作为内容——与我上面没有掩码的示例非常相似——这样的表现也很好。所以我真的只能做那样的纯色(没有渐变),但这可能是一个很好的临时解决方案。不过,我仍然希望以良好的性能实现旋转蒙版,因此欢迎提出建议:)

最佳答案

布伦特,

为什么需要使用图层蒙版?不能将 mask 层转换为子层吗?您只需要确保您的图像具有正确的 alpha 并且您将使用它的 CGImageRef 作为该层的内容。

还有一点。我还没有弄清楚为什么,但是当我在每一层而不是顶层应用 shouldRasterize 时,我也注意到了性能问题。您可能会看到在 mask 层中删除对 setShouldRasterize:YES 的引用是否有帮助。

关于iphone - Core Animation CALayer mask 动画表现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4520941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com