@implementation Taiji
{
CGFloat _cx;
CGFloat _cy;
CGFloat _r;
CGFloat _degree;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
_degree = 30;
_cx = frame.size.width/2;
_cy = frame.size.height/2;
_r = _cx - 3;
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
return self;
}
-(void)timerAction
{
_degree++;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 2);
CGContextAddEllipseInRect(ctx, CGRectMake(_cx - _r, _cy - _r, _r * 2, _r * 2));
//画一条直径
CGContextMoveToPoint(ctx,
_cx + _r * cos(toArc(_degree)),
_cy - _r * sin(toArc(_degree)));
// CGContextAddLineToPoint(ctx,
// _cx - _r * cos(toArc(_degree)),
// _cy + _r * sin(toArc(_degree)));
CGContextStrokePath(ctx);
CGContextMoveToPoint(ctx,
_cx + _r * cos(toArc(_degree)),
_cy - _r * sin(toArc(_degree)));
CGContextAddArc(ctx,
_cx + _r * cos(toArc(_degree))/2,
_cy - _r * sin(toArc(_degree))/2,
_r/2,
toArc(-_degree),
toArc(-_degree + 180),
1);
CGContextAddArc(ctx,
_cx - _r * cos(toArc(_degree))/2,
_cy + _r * sin(toArc(_degree))/2,
_r/2,
toArc(-_degree),
toArc(-_degree + 180),
0);
CGContextAddArc(ctx, _cx, _cy, _r, toArc(-_degree + 180), toArc(-_degree), 0);
CGContextAddEllipseInRect(ctx, CGRectMake(_cx + _r * cos(toArc(_degree))/2 - _r/6, _cy - _r * sin(toArc(_degree))/2- _r/6, _r / 3, _r / 3));
// CGContextStrokePath(ctx);
CGContextFillPath(ctx);
CGContextAddEllipseInRect(ctx, CGRectMake(_cx - _r * cos(toArc(_degree))/2 - _r/6, _cy + _r * sin(toArc(_degree))/2- _r/6, _r / 3, _r / 3));
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
CGContextFillPath(ctx);
}
CGFloat toArc(CGFloat degree)
{
return M_PI/180 * degree;
}
转载于:https://my.oschina.net/u/2319073/blog/2254251