Iphone 当任何其他动画发生时,CAShapeLayer会重新绘制自身?

Iphone 当任何其他动画发生时,CAShapeLayer会重新绘制自身?,iphone,ios,cocoa-touch,core-animation,core-graphics,Iphone,Ios,Cocoa Touch,Core Animation,Core Graphics,我有一个由cgmutablepath组成的图形。我将这些cgmutablepath,并根据它们所代表的图形的不同区域将它们划分为CAShapeLayers。下面是用于完成此任务的代码片段: _initialSize = _size = CGSizeMake(168.784,127.842); CGRect shapeLayerFrame = CGRectMake(0, 0, _initialSize.width, _initialSize.height); CAShapeLayer *eyeB

我有一个由
cgmutablepath
组成的图形。我将这些
cgmutablepath
,并根据它们所代表的图形的不同区域将它们划分为
CAShapeLayers
。下面是用于完成此任务的代码片段:

_initialSize = _size = CGSizeMake(168.784,127.842);
CGRect shapeLayerFrame = CGRectMake(0, 0, _initialSize.width, _initialSize.height);

CAShapeLayer *eyeBallShapeLayer = [[CAShapeLayer alloc] init];
eyeBallShapeLayer.fillColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor;
eyeBallShapeLayer.bounds = shapeLayerFrame;
CGMutablePathRef eyeBallMutablePath = CGPathCreateMutable();

CAShapeLayer *eyeWhiteShapeLayer = [[CAShapeLayer alloc] init];
eyeWhiteShapeLayer.fillColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor;
eyeWhiteShapeLayer.bounds = shapeLayerFrame;
CGMutablePathRef eyeWhiteMutablePath = CGPathCreateMutable();

CGMutablePathRef mutablePath;

mutablePath = CGPathCreateMutable();
CGPathMoveToPoint(mutablePath, NULL, 168.78,127.79);
CGPathAddLineToPoint(mutablePath, NULL, 155.56,127.13);
CGPathAddCurveToPoint(mutablePath, NULL, 156.23,126.83,156.73,125.92,157.06,125.31);
CGPathAddCurveToPoint(mutablePath, NULL, 157.60,122.36,157.84,119.11,159.77,116.67);
CGPathAddCurveToPoint(mutablePath, NULL, 160.75,115.23,160.44,113.42,160.52,111.78);
CGPathAddCurveToPoint(mutablePath, NULL, 160.58,111.83,160.64,111.88,160.70,111.94);
CGPathAddCurveToPoint(mutablePath, NULL, 161.05,112.27,161.27,112.68,161.47,113.10);
CGPathAddCurveToPoint(mutablePath, NULL, 161.63,113.46,161.78,113.82,161.96,114.16);
CGPathAddCurveToPoint(mutablePath, NULL, 161.97,114.18,161.98,114.20,161.99,114.21);
CGPathAddCurveToPoint(mutablePath, NULL, 162.13,114.47,162.29,114.71,162.51,114.92);
CGPathAddLineToPoint(mutablePath, NULL, 162.52,114.92);
CGPathAddCurveToPoint(mutablePath, NULL, 162.96,115.28,163.36,115.65,163.74,116.05);
CGPathAddCurveToPoint(mutablePath, NULL, 165.44,117.86,166.53,120.17,167.18,122.59);
CGPathAddCurveToPoint(mutablePath, NULL, 167.25,122.86,167.32,123.13,167.38,123.40);
CGPathAddCurveToPoint(mutablePath, NULL, 167.63,124.48,167.79,125.58,167.89,126.67);
CGPathAddCurveToPoint(mutablePath, NULL, 167.90,126.94,168.05,127.13,168.23,127.30);
CGPathAddCurveToPoint(mutablePath, NULL, 168.41,127.47,168.63,127.62,168.78,127.79);
CGPathCloseSubpath(mutablePath);
CGPathAddPath(eyeBallMutablePath, NULL, mutablePath);
CGPathRelease(mutablePath);


//a whole lot of paths being loaded in much the same way as above

_shapeLayers = [[NSMutableArray alloc] init];

eyeBallShapeLayer.path = eyeBallMutablePath;
[_shapeLayers addObject:eyeBallShapeLayer];
CGPathRelease(eyeBallMutablePath);
[eyeBallShapeLayer release];

eyeWhiteShapeLayer.path = eyeWhiteMutablePath;
[_shapeLayers addObject:eyeWhiteShapeLayer];
CGPathRelease(eyeWhiteMutablePath);
[eyeWhiteShapeLayer release];
我将这些Shapelayer附加到views
属性,图形绘制得很好。我遇到的问题是,当任何动画出现时,
CAShapeLayers
正在重新绘制自己。即,如果模式视图被拉上或有导航转换,则会重新绘制自己。我有一个功能,当用户点击屏幕(比如照片应用程序)时,这些条消失了,在这个过程中,它会重新绘制自己。当我拖动形状时,它也会重新绘制自身。对于我的一些有相当多路径的图形,当它重新绘制自身时,会导致动画结巴


所以我的问题是,有人知道为什么会这样吗?我做错了什么导致了这一切?当我只是在drawRect中绘制图形时,没有发生这种情况,所以我有点困惑。

默认情况下,动画中的每一帧都会重新绘制一个
CALayer
。你需要做的是告诉你的层,他们应该是。通过执行此操作,仅当层的内容发生更改时,才重新绘制层,而不是在动画中涉及层时

如果为每个形状层添加以下代码,动画应该运行得更平滑

[shapeLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
[shapeLayer setShouldRaseterize:YES];

谢谢它是否应该在动画的每一帧中都被重画?我确实尝试了你上面提到的,它有助于提高性能,但它仍然在重新绘制自己和口吃。从技术上讲,它与动画有关。为了显示/隐藏模式视图,它需要在屏幕上绘制更多或更少的图层。对于导航转换,它需要移动屏幕上绘制的位置。您可以尝试做的一件事是为放置Shapelayer的图层添加上述代码,并为Shapelayer删除这些代码。这样,只需要光栅化一层,而不是多层。