theLen = [NSBezierPath bezierPath];
[theLen moveToPoint:NSMakePoint(_frame.size.width/2 + base / 2 , _frame.size.height/2 - (diameter / 2))];
[theLen lineToPoint:NSMakePoint(_frame.size.width/2 - base / 2 , _frame.size.height/2 - (diameter / 2))];
[theLen lineToPoint:NSMakePoint(_frame.size.width/2 - (thickness / 2 ) , _frame.size.height/2)];
[theLen lineToPoint:NSMakePoint(_frame.size.width/2 - base / 2 , _frame.size.height/2 + (diameter / 2))];
[theLen lineToPoint:NSMakePoint(_frame.size.width/2 + base / 2 , _frame.size.height/2 + (diameter / 2))];
[theLen lineToPoint:NSMakePoint(_frame.size.width/2 + (thickness / 2 ) , _frame.size.height/2)];
[theLen closePath];
theLen.lineWidth = 2;
[theLen stroke];
[theLen fill];
鉴于此代码,您将如何简化它?我试图学习如何简化代码,而我所能想到的就是将其转化为......
float commonNSPoint[4];
commonNSPoint[0] = _frame.size.width/2 + base / 2;
commonNSPoint[1] = _frame.size.width/2 - base / 2;
commonNSPoint[2] = _frame.size.height/2 - (diameter / 2);
commonNSPoint[3] = _frame.size.height/2 + (diameter / 2);
theLen = [NSBezierPath bezierPath];
[theLen moveToPoint:NSMakePoint( commonNSPoint[0], commonNSPoint[2])];
[theLen lineToPoint:NSMakePoint(commonNSPoint[1], commonNSPoint[2])];
[theLen lineToPoint:NSMakePoint(_frame.size.width/2 - (thickness / 2 ) , _frame.size.height/2)];
[theLen lineToPoint:NSMakePoint(commonNSPoint[1], commonNSPoint[3])];
[theLen lineToPoint:NSMakePoint( commonNSPoint[0], commonNSPoint[3])];
[theLen lineToPoint:NSMakePoint(_frame.size.width/2 + (thickness / 2 ) , _frame.size.height/2)];
[theLen closePath];
[theLen closePath];
theLen.lineWidth = 2;
[theLen stroke];
[theLen fill];
由于NSBezierPath或UIBezierPath的点位置不完全相同,因此对我没有帮助。这也使代码变得更加混乱。
你们如何简化这样的NSBezierPath或UIBezierPath?任何事都会有所帮助(因为我的程序基于NSPoint和许多NSBezierPath绘图的不同值)。
感谢
答案 0 :(得分:3)
首先,将您的公共子表达式分解出来,并为它们指定有意义的名称(commonNSPoint
不是)。这是一个合理的开始:
CGFloat xMid = _frame.size.width / 2;
CGFloat yMid = _frame.size.height / 2;
CGFloat radius = diameter / 2;
CGFloat halfBase = base / 2;
CGFloat halfThick = thickness / 2;
然后在没有函数和方法调用的情况下创建一个只有点的数组:
CGPoint points[] = {
{ xMid + halfBase, yMid - radius },
{ xMid - halfBase, yMid - radius },
{ xMid - halfThick, yMid },
{ xMid - halfBase, yMid + radius },
{ xMid + halfBase, yMid + radius },
{ xMid + halfThick, yMid },
};
最后,创建一个包含这些点的路径:
theLen = [NSBezierPath bezierPath];
[theLen appendBezierPathWithPoints:points count:sizeof points / sizeof points[0]];
[theLen closePath];
或者,让点以原点为中心,然后在创建后转换(滑动)路径:
CGFloat radius = diameter / 2;
CGFloat halfBase = base / 2;
CGFloat halfThick = thickness / 2;
CGPoint points[] = {
{ halfBase, radius },
{ halfBase, radius },
{ halfThick, 0 },
{ halfBase, radius },
{ halfBase, radius },
{ halfThick, 0 },
};
theLen = [NSBezierPath bezierPath];
[theLen appendBezierPathWithPoints:points count:sizeof points / sizeof points[0]];
[theLen closePath];
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:_frame.size.width / 2 yBy:_frame.size.height / 2];
[theLen transformUsingAffineTransform:transform];