动画效果学习笔记:绕圆心旋转的动画

创建一个集成于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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容