iPhone:将图形路径存储为常量
我定义了一系列路径,如下所示:iPhone:将图形路径存储为常量,iphone,objective-c,ios,cocoa-touch,core-graphics,Iphone,Objective C,Ios,Cocoa Touch,Core Graphics,我定义了一系列路径,如下所示: _shapeMutablePath = CGPathCreateMutable(); CGPathMoveToPoint(_shapeMutablePath, NULL, 95.97,36.29); CGPathAddCurveToPoint(_shapeMutablePath, NULL, 96.02,29.11,90.75,23.00,83.54,21.40); CGPathAddCurveToPoint(_shapeMutablePath, NULL, 62
_shapeMutablePath = CGPathCreateMutable();
CGPathMoveToPoint(_shapeMutablePath, NULL, 95.97,36.29);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 96.02,29.11,90.75,23.00,83.54,21.40);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 62.64,66.59,64.11,66.96,65.66,67.12);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 74.52,68.04,82.49,62.03,83.47,53.69);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 83.57,52.83,83.59,51.98,83.54,51.15);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 90.74,49.56,96.01,43.45,95.97,36.29);
CGPathCloseSubpath(_shapeMutablePath);
其中一些我必须经常重复使用,所以我想知道存储和检索这些信息的最佳方式是什么?可以在我的常量文件中将其保存为常量吗?创建路径的计算时间与在屏幕上绘制路径的时间相比微不足道,因此使用静态方法或其他方法按需创建路径应该不会有问题
或者,您可以创建一个静态类来存储要重复使用的不同路径,您可以像这样进行延迟加载 添加属性
@property (nonatomic, assign, readonly) CGMutablePathRef shapeMutablePath;
然后重写getter
- (CGMutablePathRef)shapeMutablePath;
{
if (!_shapeMutablePath) {
_shapeMutablePath = CGPathCreateMutable();
CGPathMoveToPoint(_shapeMutablePath, NULL, 95.97,36.29);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 96.02,29.11,90.75,23.00,83.54,21.40);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 62.64,66.59,64.11,66.96,65.66,67.12);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 74.52,68.04,82.49,62.03,83.47,53.69);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 83.57,52.83,83.59,51.98,83.54,51.15);
CGPathAddCurveToPoint(_shapeMutablePath, NULL, 90.74,49.56,96.01,43.45,95.97,36.29);
CGPathCloseSubpath(_shapeMutablePath);
}
return _shapeMutablePath;
}
您还需要在dealloc进行清理
- (void)dealloc;
{
CGPathRelease(_shapeMutablePath);
}