创建一个集成于UIView的类,里面的代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
//  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

Untitled.gif

路径是不会显示的,画圆是为了显示效果。