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绘图)。
谢谢
发布于 2016-03-05 15:36:17
首先,提取出通用子表达式,并为它们指定有意义的名称( 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];https://stackoverflow.com/questions/35534708
复制相似问题