【问题标题】:Simplifying NSBezierPath (Or UIBezierPath) code, How?简化 NSBezierPath(或 UIBezierPath)代码,如何?
【发布时间】:2016-02-21 10:00:13
【问题描述】:
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 绘图)。

谢谢

【问题讨论】:

    标签: objective-c uibezierpath nsbezierpath


    【解决方案1】:

    首先,分解出你的常用子表达式并给它们起有意义的名字(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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多