首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何简化NSBezierPath (或UIBezierPath)代码?

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

Stack Overflow用户
提问于 2016-02-21 18:00:13
回答 1查看 211关注 0票数 1
代码语言:javascript
复制
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];

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

代码语言:javascript
复制
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绘图)。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-03-05 15:36:17

首先,提取出通用子表达式,并为它们指定有意义的名称( commonNSPoint不是)。这是一个合理的开始:

代码语言:javascript
复制
    CGFloat xMid = _frame.size.width / 2;
    CGFloat yMid = _frame.size.height / 2;
    CGFloat radius = diameter / 2;
    CGFloat halfBase = base / 2;
    CGFloat halfThick = thickness / 2;

然后创建一个只包含点的数组,而不使用函数和方法调用来使事情变得混乱:

代码语言:javascript
复制
    CGPoint points[] = {
        { xMid + halfBase,  yMid - radius },
        { xMid - halfBase,  yMid - radius },
        { xMid - halfThick, yMid },
        { xMid - halfBase,  yMid + radius },
        { xMid + halfBase,  yMid + radius },
        { xMid + halfThick, yMid },
    };

最后,创建包含这些点的路径:

代码语言:javascript
复制
    theLen = [NSBezierPath bezierPath];
    [theLen appendBezierPathWithPoints:points count:sizeof points / sizeof points[0]];
    [theLen closePath];

或者,让点围绕原点居中,然后在创建路径后平移(滑动)该路径:

代码语言:javascript
复制
    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];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35534708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档