创建一个集成于UIView的类,里面的代码如下
//
// 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
路径是不会显示的,画圆是为了显示效果。
网友评论