人生在世,永远也不该演戏作假 --《异乡人》
创建一个集成于UIView的类,里面的代码如下1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677//// GraphicView.m// aestest//// Created by jadefan on 16/3/30.// Copyright © 2016年 jadefan. All rights reserved.//#import "GraphicView.h"#import <QuartzCore/QuartzCore.h>@interface GraphicView(){ UIView *subView;}@end@implementation GraphicView- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; subView.backgroundColor = [UIColor orangeColor]; [self addSubview:subView]; } return self;}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code //获取绘图的工具 CGContextRef ctx = UIGraphicsGetCurrentContext(); //设置绘图的线的宽度 CGContextSetLineWidth(ctx, 2); //画圆,画圆的参数 CGContextAddArc(ctx, 100, 20, 15, 0, 2*M_PI, 0); //开始画圆,设置填充模式 CGContextDrawPath(ctx,kCGPathStroke); //生成一个path CGMutablePathRef path = CGPathCreateMutable(); //指定这个pathd的路径,这个路径是看不见的,但是我们上面画了出啦 CGPathAddArc(path, NULL, 100, 20, 15, 0, 2*M_PI, YES); //关键帧动画,position代表这个动画是按着点移动的 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; //动画持续时间 animation.duration = 1.0f; //动画的效果, animation.timingFunction = [CAMediaTimingFunction functionWithName:@"linear"]; //重复次数,设置大点达到循环的效果 animation.repeatCount = 1000; //制定上面设置好的路径 animation.path = path; //添加到需要做动画的视图的layer上面 [subView.layer addAnimation:animation forKey:@"path"]; }@end
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
//// GraphicView.m// aestest//// Created by jadefan on 16/3/30.// Copyright © 2016年 jadefan. All rights reserved.//#import "GraphicView.h"#import <QuartzCore/QuartzCore.h>@interface GraphicView(){ UIView *subView;}@end@implementation GraphicView- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; subView.backgroundColor = [UIColor orangeColor]; [self addSubview:subView]; } return self;}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code //获取绘图的工具 CGContextRef ctx = UIGraphicsGetCurrentContext(); //设置绘图的线的宽度 CGContextSetLineWidth(ctx, 2); //画圆,画圆的参数 CGContextAddArc(ctx, 100, 20, 15, 0, 2*M_PI, 0); //开始画圆,设置填充模式 CGContextDrawPath(ctx,kCGPathStroke); //生成一个path CGMutablePathRef path = CGPathCreateMutable(); //指定这个pathd的路径,这个路径是看不见的,但是我们上面画了出啦 CGPathAddArc(path, NULL, 100, 20, 15, 0, 2*M_PI, YES); //关键帧动画,position代表这个动画是按着点移动的 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; //动画持续时间 animation.duration = 1.0f; //动画的效果, animation.timingFunction = [CAMediaTimingFunction functionWithName:@"linear"]; //重复次数,设置大点达到循环的效果 animation.repeatCount = 1000; //制定上面设置好的路径 animation.path = path; //添加到需要做动画的视图的layer上面 [subView.layer addAnimation:animation forKey:@"path"]; }@end
路径是不会显示的,画圆是为了显示效果。