首页 > 代码库 > 画太极图

画太极图

画太极图

{
    CGFloat _degree;//表示当前的角度
    CGFloat _r;//表示半径
    CGFloat _R;//view的宽度/2
    CGFloat _eye;//孔
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _degree = 30;
        _r = frame.size.width/2 - 1;
        _R = frame.size.width/2;
        _eye = _r/6;//小圆
    }
    return self;
}

float degreeToRadian(float degree) {
    return  M_PI / 180 * degree;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef crf = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(crf, CGRectMake(1, 1, _r * 2, _r * 2));
    CGContextStrokePath(crf);
    //右上角的半圆
    CGContextAddArc(crf, _R + _r * cos(degreeToRadian(_degree))/2, _R - _r * sin(degreeToRadian(_degree))/2, _r/2, degreeToRadian(-_degree), degreeToRadian(-_degree + 180), 0);
    //左下角的半圆
    CGContextAddArc(crf, _R - _r * cos(degreeToRadian(_degree))/2, _R + _r * sin(degreeToRadian(_degree))/2, _r/2, degreeToRadian(-_degree), degreeToRadian(-_degree + 180), 1);
    //大圆,回到起点
    CGContextAddArc(crf, _R, _R, _r, degreeToRadian(-_degree + 180), degreeToRadian(-_degree), 1);
    
    CGContextAddEllipseInRect(crf, CGRectMake(_R + _r * cos(degreeToRadian(_degree))/2 - _eye, _R - _r * sin(degreeToRadian(_degree))/2 - _eye, _eye * 2, _eye * 2));
    CGContextAddEllipseInRect(crf, CGRectMake(_R - _r * cos(degreeToRadian(_degree))/2 - _eye, _R + _r *sin(degreeToRadian(_degree))/2 - _eye, _eye * 2, _eye * 2));
    CGContextFillPath(crf);
}


画太极图