画图2

定时器画图


- (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);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容