Iphone 如何使用UIBezierPath在cocoa touch中绘制多边形
我想画不同边的多边形(4-12)。绘制多边形的逻辑是什么。例如,如果用户选择6边,它应该画一个六边形,如果用户输入8边,它应该画一个八角形。我找到了以下代码,但我还想调整我正在绘制多边形的UIView的大小,以便视图内部的形状也随着视图的增长而增长。任何人都可以帮助我。以下是我当前正在使用的代码,但当我调整视图大小时,它也不会位于中心,因为形状会移动到视图中的另一个位置Iphone 如何使用UIBezierPath在cocoa touch中绘制多边形,iphone,ios,objective-c,cocoa-touch,uibezierpath,Iphone,Ios,Objective C,Cocoa Touch,Uibezierpath,我想画不同边的多边形(4-12)。绘制多边形的逻辑是什么。例如,如果用户选择6边,它应该画一个六边形,如果用户输入8边,它应该画一个八角形。我找到了以下代码,但我还想调整我正在绘制多边形的UIView的大小,以便视图内部的形状也随着视图的增长而增长。任何人都可以帮助我。以下是我当前正在使用的代码,但当我调整视图大小时,它也不会位于中心,因为形状会移动到视图中的另一个位置 int radius = MINIMUM(widht, height)*0.4 ; for (int i
int radius = MINIMUM(widht, height)*0.4 ;
for (int i = 0; i < _numberOFsides; i++){
CGPoint point = CGPointMake(widht/2+radius *cosf(i*2*M_PI/_numberOFsides), widht/2+radius*sinf(i*2*M_PI/_numberOFsides));
if (i==0) {
[_shapePath moveToPoint:point];
}
else{
[_shapePath addLineToPoint:point];
[_shapePath stroke];
}
}
int半径=最小值(宽、高)*0.4;
对于(int i=0;i<\u numberOFsides;i++){
CGPoint-point=CGPointMake(宽/2+半径*cosf(i*2*M_-PI/_-numberOFsides),宽/2+半径*sinf(i*2*M_-PI/_-numberOFsides));
如果(i==0){
[_shapePath移动点:点];
}
否则{
[_shapepathaddlinetopoint:point];
[_形路径笔划];
}
}
现在要调整您的UIBazierPath,您可以添加以下代码
CGRect bazierRect = CGPathGetBoundingBox(bezierpath.CGPath)
CGFloat scaleX = view.frame.size.width / bazierRect.frame.size.width;
CGFloat scaleY = view.frame.size.height / bazierRect.frame.size.height;
CGAffineTransform transform = CGAffineTransformMakeScale(scaleX, scaleY);
CGPathRef newPath = CGPathCreateCopyByTransformingPath(bezierpath.CGPath, &transform);
bezierPath.CGPath = newPath;
CFRelease(newPath);
现在,要调整UIBazierPath的大小,您可以添加以下代码
CGRect bazierRect = CGPathGetBoundingBox(bezierpath.CGPath)
CGFloat scaleX = view.frame.size.width / bazierRect.frame.size.width;
CGFloat scaleY = view.frame.size.height / bazierRect.frame.size.height;
CGAffineTransform transform = CGAffineTransformMakeScale(scaleX, scaleY);
CGPathRef newPath = CGPathCreateCopyByTransformingPath(bezierpath.CGPath, &transform);
bezierPath.CGPath = newPath;
CFRelease(newPath);
如果要创建任意边数的正多边形,以下代码将为您提供每条边的顶点,并且可以轻松地重新缩放边的大小和数量:
int n = 10; //number of edges
float j = 20; //length of each edge
float x = 130;
float y = 250;//the point 130,250 will be at the bottom of the figure
float angle = 2*M_PI;
for (int i = 0; i < n; i++) {
CGRect frame = CGRectMake(x, y, 2, 2);//put a dot on x,y
NSLog(@"%f | %f, %f", angle, x, y);
x = x + j*cosf(angle);
y = y + j*sinf(angle); //move to the next point
angle = angle - 2*M_PI/n; //update the angle
//display the dot
UIView *rect = [[UIView alloc] initWithFrame:frame];
rect.backgroundColor = [UIColor blueColor];
[self.view addSubview:rect];
}
int n=10//边数
浮动j=20//每条边的长度
浮点数x=130;
浮动y=250//点130250将位于图的底部
浮动角度=2*M_PI;
对于(int i=0;i
希望这有帮助。如果您有任何问题,请随时提问,祝您度过愉快的一天
~Detaly Porcupine如果要创建任意边数的正多边形,以下代码将为您提供每条边的顶点,并且可以轻松地重新缩放大小和边数:
int n = 10; //number of edges
float j = 20; //length of each edge
float x = 130;
float y = 250;//the point 130,250 will be at the bottom of the figure
float angle = 2*M_PI;
for (int i = 0; i < n; i++) {
CGRect frame = CGRectMake(x, y, 2, 2);//put a dot on x,y
NSLog(@"%f | %f, %f", angle, x, y);
x = x + j*cosf(angle);
y = y + j*sinf(angle); //move to the next point
angle = angle - 2*M_PI/n; //update the angle
//display the dot
UIView *rect = [[UIView alloc] initWithFrame:frame];
rect.backgroundColor = [UIColor blueColor];
[self.view addSubview:rect];
}
int n=10//边数
浮动j=20//每条边的长度
浮点数x=130;
浮动y=250//点130250将位于图的底部
浮动角度=2*M_PI;
对于(int i=0;i
希望这有帮助。如果您有任何问题,请随时提问,祝您度过愉快的一天
~Detaly Porcupine您是否正在UIView的drawRect中编写上述代码?并根据uiview的矩形更新高度和宽度?…每次调整视图大小时调用setneedsdisplay。是。我在UIView的drawrect中写作,每次都调用setneedsdisplay。我不知道逻辑出了什么问题。为什么不先尝试传递静态值,然后再尝试用基本的方法,比如UIBezierPath*aPath=[UIBezierPath-bezierPath];//设置形状的起点。[aPath moveToPoint:CGPointMake(100.0,0.0)];//划清界限。[aPath addLineToPoint:CGPointMake(200.0,40.0)];[aPath addLineToPoint:CGPointMake(160140)];[aPath addLineToPoint:CGPointMake(40.0140)];[aPath addLineToPoint:CGPointMake(0.0,40.0)];[aPath closePath];我先取了静态值,效果很好。但是我希望形状随着视图的增长而增长,所以我给出了动态的值?并根据uiview的矩形更新高度和宽度?…每次调整视图大小时调用setneedsdisplay。是。我在UIView的drawrect中写作,每次都调用setneedsdisplay。我不知道逻辑出了什么问题。为什么不先尝试传递静态值,然后再尝试用基本的方法,比如UIBezierPath*aPath=[UIBezierPath-bezierPath];//设置形状的起点。[aPath moveToPoint:CGPointMake(100.0,0.0)];//划清界限。[aPath addLineToPoint:CGPointMake(200.0,40.0)];[aPath addLineToPoint:CGPointMake(160140)];[aPath addLineToPoint:CGPointMake(40.0140)];[aPath addLineToPoint:CGPointMake(0.0,40.0)];[aPath closePath];我先取了静态值,效果很好。但是我希望形状随着视图的增长而增长,所以我给出了动态的值。