【问题标题】:drawRect - Draw StardrawRect - 绘制星形
【发布时间】:2013-08-16 07:31:34
【问题描述】:

我正在视图上绘制注释。我用来画星星的方法如下。

-(void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGFloat xCenter = rect.size.width / 2;
CGFloat yCenter = rect.size.height / 2;

float width;
if(rect.size.width > rect.size.height) {
    width = rect.size.height;
} else {
    width = rect.size.width;
}

double r = width / 2.0;
float flip = -1.0;

double theta = 2.0 * M_PI * (2.0 / 5.0); // 144 degrees

CGContextMoveToPoint(context, xCenter, r*flip+yCenter);

for (NSUInteger k=1; k<5; k++)
{
    float x = r * sin(k * theta);
    float y = r * cos(k * theta);
    CGContextAddLineToPoint(context, x+xCenter, y*flip+yCenter);
}

CGContextSetStrokeColorWithColor(context, self.color.CGColor);

CGContextClosePath(context);
CGContextStrokePath(context);

}

我想要去除其他内线的星星边框有什么方法可以实现吗? (除了使用 moveToPoint 和 addLine 方法)

【问题讨论】:

  • 既然你知道星星的边界在哪里,我能想到的就是清除它内部的路径。
  • 例如,您将计算其余 5 个内部点,并使用 10 个点制作星形多边形...
  • 星的中心有一个倒五边形。我需要得到它的分数,你是这个意思吗?
  • This answer 包含一个用于绘制星形轮廓的 Java 函数。将其移植到 iOS 应该很容易。

标签: ios drawing core-graphics


【解决方案1】:

使用此代码:

-(void)drawRect:(CGRect)rect
{
    int aSize = 100.0;
    float color[4] = { 0.0, 0.0, 1.0, 1.0 }; // Blue
    CGColorRef aColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), color);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, aSize);
    CGFloat xCenter = 100.0;
    CGFloat yCenter = 100.0;

    float  w = 100.0;
    double r = w / 2.0;
    float flip = -1.0;

    CGContextSetFillColorWithColor(context, aColor);


    CGContextSetStrokeColorWithColor(context, aColor);

    double theta = 2.0 * M_PI * (2.0 / 5.0); // 144 degrees

    CGContextMoveToPoint(context, xCenter, r*flip+yCenter);

    for (NSUInteger k=1; k<5; k++) 
    {
        float x = r * sin(k * theta);
        float y = r * cos(k * theta);
        CGContextAddLineToPoint(context, x+xCenter, y*flip+yCenter);
    }

    CGContextClosePath(context);
    CGContextFillPath(context);
    }

这将创建 1 个蓝星

【讨论】:

    【解决方案2】:

    使用UIBezierPath,您可以在Swift 4 中使用它。

    let polygonPath = UIBezierPath()
    
    let xCenter: CGFloat = 0
    let yCenter: CGFloat = 0
    
    let w = CGFloat(300)
    let r = w / 2.0
    let flip: CGFloat = -1.0 // use this to flip the figure 1.0 or -1.0
    
    let polySide = CGFloat(5)
    
    let theta = 2.0 * Double.pi * Double(2.0 / polySide)
    
    polygonPath.move(to: CGPoint(x: xCenter, y: r * flip + yCenter))
    
    for i in 1..<Int(polySide) {
        let x: CGFloat = r * CGFloat( sin(Double(i) * theta) )
        let y: CGFloat = r * CGFloat( cos(Double(i) * theta) )
        polygonPath.addLine(to: CGPoint(x: x + xCenter, y: y * flip + yCenter))
    }
    
    polygonPath.close()
    

    【讨论】:

    • 不错的解决方案!但是在这种情况下,绘图不能用默认的 context.fillPath() 填充?
    【解决方案3】:

    您可以使用以下代码绘制星星:

    UIBezierPath* starPath = UIBezierPath.bezierPath;
     [starPath moveToPoint: CGPointMake(90, 31)];
    [starPath addLineToPoint: CGPointMake(98.11, 42.06)];
    [starPath addLineToPoint: CGPointMake(111.87, 45.86)];
    [starPath addLineToPoint: CGPointMake(103.12, 56.49)];
    [starPath addLineToPoint: CGPointMake(103.52, 69.89)];
    [starPath addLineToPoint: CGPointMake(90, 65.4)];
    [starPath addLineToPoint: CGPointMake(76.48, 69.89)];
    [starPath addLineToPoint: CGPointMake(76.88, 56.49)];
    [starPath addLineToPoint: CGPointMake(68.13, 45.86)];
    [starPath addLineToPoint: CGPointMake(81.89, 42.06)];
    [starPath closePath];
    [UIColor.grayColor setFill];
    [starPath fill];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多