我试图仅使用文字标签在UIView
中绘制螺旋线。代码如下:
//
// ViewController.m
// spiral
//
// Created on 30/01/2015.
// Copyright (c) 2015 All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIView *canvas;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self plotPoints:self.view.frame.size.width/2 andY:self.view.frame.size.height/2];
// centerX-- X origin of the spiral.
// centerY-- Y origin of the spiral.
// radius--- Distance from origin to outer arm.
// sides---- Number of points or sides along the spiral's arm.
// coils---- Number of coils or full rotations. (Positive numbers spin clockwise, negative numbers spin counter-clockwise)
// rotation- Overall rotation of the spiral. ('0'=no rotation, '1'=360 degrees, '180/360'=180 degrees)
//
[self spiral:170 andY:284 andRadius:100 withSides:20 andCoils:10 andRotation:0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) spiral: (float)centerX andY:(float)centerY andRadius:(float)radius withSides: (float)sides andCoils:(float)coils andRotation:(float)rotation
{
//
// How far to step away from center for each side.
float awayStep = radius/sides;
//
// How far to rotate around center for each side.
float aroundStep = coils/sides;// 0 to 1 based.
//
// Convert aroundStep to radians.
float aroundRadians = aroundStep * 2 * M_PI;
//
// Convert rotation to radians.
rotation *= 2 * M_PI;
//
// For every side, step around and away from center.
for(int i=1; i<=sides; i++){
//
// How far away from center
float away = i * awayStep;
//
// How far around the center.
float around = i * aroundRadians + rotation;
//
// Convert 'around' and 'away' to X and Y.
float x = centerX + cos(around) * away;
float y = centerY + sin(around) * away;
//
[self plotPoints:x andY:y];
}
}
-(void)plotPoints: (float)x andY:(float) y {
UILabel *point = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
point.font= [UIFont systemFontOfSize:19];
point.text=@"*";
point.center = CGPointMake(x, y);
[self.canvas addSubview:point];
point.hidden=NO;
}
@end
截图如下:
不确定为什么会这样做。可以在代码中一直使用浮点值吗?也许我在三角形中失去了一些东西。任何帮助,将不胜感激。谢谢!
编辑:查看x,y值:
2015-01-31 00:02:32.073 spiral[86741:6997860] x: 165.000000, y:284.000000
2015-01-31 00:02:32.075 spiral[86741:6997860] x: 180.000000, y:284.000000
2015-01-31 00:02:32.076 spiral[86741:6997860] x: 155.000000, y:284.000000
2015-01-31 00:02:32.076 spiral[86741:6997860] x: 190.000000, y:284.000000
2015-01-31 00:02:32.077 spiral[86741:6997860] x: 145.000000, y:283.999969
2015-01-31 00:02:32.077 spiral[86741:6997860] x: 200.000000, y:284.000000
2015-01-31 00:02:32.078 spiral[86741:6997860] x: 135.000000, y:283.999939
2015-01-31 00:02:32.078 spiral[86741:6997860] x: 210.000000, y:284.000031
2015-01-31 00:02:32.079 spiral[86741:6997860] x: 125.000000, y:284.000000
2015-01-31 00:02:32.079 spiral[86741:6997860] x: 220.000000, y:284.000061
2015-01-31 00:02:32.080 spiral[86741:6997860] x: 115.000000, y:283.999847
2015-01-31 00:02:32.080 spiral[86741:6997860] x: 230.000000, y:284.000000
2015-01-31 00:02:32.081 spiral[86741:6997860] x: 105.000000, y:283.999908
2015-01-31 00:02:32.081 spiral[86741:6997860] x: 240.000000, y:284.000183
2015-01-31 00:02:32.081 spiral[86741:6997860] x: 95.000000, y:284.000000
2015-01-31 00:02:32.082 spiral[86741:6997860] x: 250.000000, y:284.000122
2015-01-31 00:02:32.160 spiral[86741:6997860] x: 85.000000, y:283.999786
2015-01-31 00:02:32.160 spiral[86741:6997860] x: 260.000000, y:284.000000
2015-01-31 00:02:32.161 spiral[86741:6997860] x: 75.000000, y:283.999878
2015-01-31 00:02:32.161 spiral[86741:6997860] x: 270.000000, y:284.000275
似乎y值无法正确计算..
固定图片:
答案 0 :(得分:2)
如果您在degress中输入角度,则正确转换为弧度为:
float radians = degrees * M_PI / 180.0f;
您对该功能的输入似乎也是错误的。
float aroundStep = coils/sides;// 0 to 1 based.
此评论表明coils
&lt; sides
。您输入10
和5
,aroundStep = 2
;不在0
和1
之间。