我有一张由CGMutablePaths
组成的图片。我正在使用这些CGMutablePaths
,并根据它们所代表的图形的不同区域将它们分成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];
我将这些shapeLayers附加到视图layer
属性,图形绘制正常。我得到的问题是CAShapeLayers
在发生任何动画时都会重绘自己。 IE如果提升模态视图或进行导航转换,则会重绘自身。我有一些功能,当用户点击屏幕(如照片应用程序)时,栏会消失,并在此期间重绘自己。当我拖动形状时它也会自动重绘。对于我的一些具有相当数量路径的图形,它会导致动画在重绘自身时断断续续。
所以我的问题是,有谁知道为什么会这样?我做错了什么导致这种情况发生?当我只是在drawRect中绘制图形时没有发生这种情况,所以我有点困惑。
答案 0 :(得分:0)
默认情况下,动画中的每一帧都会重绘CALayer
。您需要做的是告诉您的图层应该rasterized。通过这样做,只有在图层内容发生变化时才会重新绘制图层,而不是在动画中涉及它时。
如果为每个形状图层添加以下代码,动画应该运行得更顺畅。
[shapeLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
[shapeLayer setShouldRaseterize:YES];