实战以中心点绘制圆点并显示文字

来源:互联网 发布:淘宝动漫周边 侵权 编辑:程序博客网 时间:2024/05/21 23:32

网上看了些资料,整理并实践了标题所说的问题.分享出来.

先上效果图


代码如下

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        _lineWidth = 20;

        _radius = 40.0;//

        _myColor = [UIColorcolorWithRed:255/255.0fgreen:0/255.0fblue:0/255.0falpha:1.0f];

    }

    return self;

}


#pragma mark - drawing methods

- (void)drawRect:(CGRect)rect

{

    [super drawRect:rect];

    

    CGContextRef tx = UIGraphicsGetCurrentContext();

    

    //绘制圆

    [self drawRadar:tx];

    

    //绘制圆上文字

    [self drawMyLabel:tx];

}

-(void) drawRadar:(CGContextRef)tx{

    CGContextSaveGState(tx);

    [_myColor set];

    for (int i = 0; i < 10; i++) {

        CGPoint point =  [selfpointFromAngle:36*i];

        CGContextFillEllipseInRect(tx,CGRectMake(point.x, point.y,_lineWidth,_lineWidth));

    }

    CGContextRestoreGState(tx);

}

-(void) drawMyLabel:(CGContextRef)tx {

    NSDictionary *dic = @{ NSFontAttributeName: [UIFontsystemFontOfSize:9.0],

                           NSForegroundColorAttributeName: [UIColorwhiteColor]};

    int pad = 0;

    for (int i=0; i<10; i++) {

        NSString* text = [NSStringstringWithFormat:@"%@%.2d",NSLocalizedString(@"A",@"A"),i + 1];

        CGPoint curPoint = [selfpointFromAngle:36*i];

        CGRect textRect = CGRectMake(curPoint.x, curPoint.y, [selfwidthOfString:textwithFont:[UIFontsystemFontOfSize:9.0]], [selfheightOfString:textwithFont:[UIFontsystemFontOfSize:9.0]]);

        float center = ToRad(AngleFromNorth(CGPointMake(self.frame.size.width/2,self.frame.size.height/2), curPoint,NO));

        textRect.origin.x =  (textRect.origin.x + pad *cos(center)) - textRect.size.width/4 +5.0;

        textRect.origin.y = (textRect.origin.y + pad *sin(center))- textRect.size.height/4 +5.0;

        [text drawInRect:textRect withAttributes:dic];

    }

}

//计算逆时针方向的角度对应的坐标

-(CGPoint)pointFromAngle:(int)angleInt{

    //Define the Circle center

    CGPoint centerPoint = CGPointMake(self.frame.size.width/2 -_lineWidth/2,self.frame.size.height/2 -_lineWidth/2);

    

    //Define The point position on the circumference

    CGPoint result;

    result.y = round(centerPoint.y +_radius *sin(ToRad(-angleInt-90)));

    result.x = round(centerPoint.x +_radius *cos(ToRad(-angleInt-90)));

    return result;

}

static inline float AngleFromNorth(CGPoint p1, CGPoint p2, BOOL flipped) {

    CGPoint v = CGPointMake(p2.x-p1.x,p2.y-p1.y);

    float vmag = sqrt(SQR(v.x) +SQR(v.y)), result =0;

    v.x /= vmag;

    v.y /= vmag;

    double radians = atan2(v.y,v.x);

    result = ToDeg(radians);

    return (result >=0  ? result : result +360.0);

}

- (CGFloat) widthOfString:(NSString *)string withFont:(UIFont*)font {

    NSDictionary *attributes = [NSDictionarydictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];

    return [[[NSAttributedStringalloc]initWithString:string attributes:attributes]size].width;

}

- (CGFloat) heightOfString:(NSString *)string withFont:(UIFont*)font {

    NSDictionary *attributes = [NSDictionarydictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];

    return [[[NSAttributedStringalloc]initWithString:string attributes:attributes]size].height;

}


0 0
原创粉丝点击