Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何简化NSBezierPath(或UIBezierPath)代码?_Objective C_Uibezierpath_Nsbezierpath - Fatal编程技术网

Objective c 如何简化NSBezierPath(或UIBezierPath)代码?

Objective c 如何简化NSBezierPath(或UIBezierPath)代码?,objective-c,uibezierpath,nsbezierpath,Objective C,Uibezierpath,Nsbezierpath,有了这段代码,您将如何简化它?我正在努力学习如何简化代码,我所能想到的就是把它变成 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 ,

有了这段代码,您将如何简化它?我正在努力学习如何简化代码,我所能想到的就是把它变成

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];
这对我没什么帮助,因为NSBezierPath或UIBezierPath的点位置并不完全相同。这也使得代码看起来更混乱

你们如何简化像这样的NSBezierPath或UIBezierPath。任何东西都会有帮助(因为我的程序基于NSPoint的不同值和许多NSBezierPath绘图)


谢谢

首先,找出您常用的子表达式,并给它们起一个有意义的名字(commonpoint不是)。这是一个合理的开始:

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];
然后创建一个只包含点的数组,而不让函数和方法调用造成混乱:

    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];