Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.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
Ios drawRect-Draw Star_Ios_Drawing_Core Graphics - Fatal编程技术网

Ios drawRect-Draw Star

Ios drawRect-Draw Star,ios,drawing,core-graphics,Ios,Drawing,Core Graphics,我正在视图上绘制注释。下面是我用来画星星的方法 -(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.si

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

-(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);

}
-(void)drawRect:(CGRect)rect{
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(上下文,2.0);
CGFloat xCenter=rect.size.width/2;
cGycenter=rect.size.height/2;
浮动宽度;
if(rect.size.width>rect.size.height){
宽度=矩形尺寸高度;
}否则{
宽度=rect.size.width;
}
双r=宽度/2.0;
浮动翻转=-1.0;
双θ=2.0*M_π*(2.0/5.0);//144度
CGContextMoveToPoint(上下文、xCenter、r*flip+yCenter);
对于(k=1;k整数)使用以下代码:

-(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);
    }
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];
-(void)drawRect:(CGRect)rect
{
int aSize=100.0;
浮动颜色[4]={0.0,0.0,1.0,1.0};//蓝色
CGColorRef aColor=CGColorCreate(CGColorSpaceCreateDeviceRGB(),color);
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(上下文,aSize);
cGxCenter=100.0;
cGycenter=100.0;
浮球w=100.0;
双r=w/2.0;
浮动翻转=-1.0;
CGContextSetFillColorWithColor(上下文,A颜色);
CGContextSetStrokeColorWithColor(上下文,A颜色);
双θ=2.0*M_π*(2.0/5.0);//144度
CGContextMoveToPoint(上下文、xCenter、r*flip+yCenter);

对于(k=1;k整数),可以使用以下代码绘制星形:

-(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);
    }
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];

使用
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()
let polygonPath=UIBezierPath()
让xCenter:CGFloat=0
设yCenter:CGFloat=0
设w=CGFloat(300)
设r=w/2.0
让我们翻转:CGFloat=-1.0//使用它来翻转图1.0或-1.0
让polySide=CGFloat(5)
设θ=2.0*Double.pi*Double(2.0/polySide)
polygonPath.move(到:CGPoint(x:xCenter,y:r*flip+yCenter))

因为你知道恒星的边界在哪里,所以我能想到的就是清除它内部的路径。例如,你可以计算剩余的5个内部点,然后用10个点组成恒星多边形…恒星的中心有一个倒五边形。我需要得到它的点,你的意思是吗?包含一个Java函数只绘制星形的轮廓。将其移植到iOS应该很容易。这是一个不错的解决方案!但在这种情况下,绘制它不能用默认上下文填充。fillPath()