0

好的,这是我的问题,

假设您的方位角为 80 度。我想用一个简单的 10x10 正方形在 iPhone 屏幕上绘制出来。让我们计算一下纵向模式下 iPhone 的顶部是北。

关于我将如何做到这一点的任何想法?

顺便说一句 - 我不想使用以下方法:

CGAffineTransform where  = CGAffineTransformMakeRotation(degreesToRadians(x_rounded));
[self.compassContainer2 setTransform:where];

我想通过在 iPhone 屏幕上设置 X -Y 线手动在屏幕上绘图。

4

2 回答 2

1
- (void)drawRect:(CGRect)rect
{
    float compass_bearing = 80.0;  // 0 = North, 90 = East, etc.

    CGContextRef theContext = UIGraphicsGetCurrentContext();
    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 5.0, 5.0);
    CGPathAddLineToPoint(path, NULL,
        5.0 + 5.0 * cos((compass_bearing - 90.0) * M_PI / 180.0),
        5.0 + 5.0 * sin((compass_bearing - 90.0) * M_PI / 180.0));

    CGContextSetLineWidth(theContext, 2.0);
    CGContextSetStrokeColorWithColor(theContext, [UIColor blackColor].CGColor);
    CGContextAddPath(theContext, path);
    CGContextStrokePath(theContext);

    CGPathRelease(path);
}
于 2012-06-19T23:11:54.460 回答
1

所以在我看来,你想要完成的事情应该存在于drawRect自定义视图的方法中,然后这个视图将通过你想要的任何方法(即故事板或以编程方式)添加到你的屏幕上。这是一个可能的实现,您可以使用它根据某个“角度”从视图中心绘制一条直线:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    // Drawing code
    CGFloat angle = 0.0 /180.0 * M_PI ;

    //Set this to be the length from the center
    CGFloat lineDist = 320.0;
    CGContextSetLineWidth(context,  5.0);
    //Set Color
    [[UIColor redColor] setStroke];

    //Draw the line
    CGContextBeginPath(context);
    //Move to center
    CGContextMoveToPoint(context, self.frame.size.width/2, self.frame.size.height/2);

    //Draw line based on unit circle
    //Calculate point based on center starting point, and some movement from there based on the angle.
    CGFloat xEndPoint = lineDist * sin(angle) + self.frame.size.width/2;
    //Calculate point based on center starting point, and some movement from there based on the angle. (0 is the top of the view, so want to move up when your answer is negative)    
    CGFloat yEndPoint = -lineDist * cos(angle) + self.frame.size.height/2;

    CGContextAddLineToPoint(context, xEndPoint, yEndPoint);

    CGContextStrokePath(context);
}
于 2012-06-19T23:12:48.077 回答