定时器画图
- (id)initWithFrame:(CGRect)frame {
self= [super initWithFrame:frame];
if(self) {
_movePoint= CGPointMake(10, 10);
_linePoint = CGPointMake(10, 10);
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
return self;
}
- (void)onTimer {
CGPoint movePoint =_movePoint;
movePoint.x += 10;
movePoint.y += 10;
_movePoint = movePoint;
CGPoint linePoint = _linePoint;
linePoint.x += 5;
linePoint.y += 20;
_linePoint = linePoint;
//调用drawRect方法重新绘制
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//创建画布获取当前画板
CGContextRefcontext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10);
CGContextSetRGBStrokeColor(context, 1, 1, 0, 1);
CGContextSetLineJoin(context,kCGLineJoinRound);
CGContextMoveToPoint(context,_movePoint.x,_movePoint.y);
CGContextAddLineToPoint(context,_linePoint.x,_linePoint.y);
CGContextStrokePath(context);
}