登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

CAKeyFrameAnimation的动画Demo

[复制链接]
来自: zwz-coder 分类: iOS精品源码 上传时间: 2016-6-14 20:42:58

项目介绍:

CAKeyFrameAnimation的动画Demo

主要实现方法:
[Objective-C] 查看源文件 复制代码
#import "JTBorderDotAnimation.h"

@interface JTBorderDotAnimation(){
    NSMutableArray *pointsViews;
}

@end

@implementation JTBorderDotAnimation

- (id)init
{
    self = [super init];
    if(!self){
        return nil;
    }
    
    [self commonInit];
    
    return self;
}

- (void)commonInit
{
    self.numberPoints = 2;
    
    self.duration = 5.;
    self.repeatCount = HUGE_VALF;
    
    self.pointSize = 4.;
    self.pointColor = [UIColor blueColor];
    
    pointsViews = [NSMutableArray new];
}

- (void)start
{
    if(!self.animatedView){
        NSLog(@"JTBorderDotAnimation no view to animate");
        return;
    }
 
    [self stop];
    
    self.animatedView.clipsToBounds = NO;
    
    CFTimeInterval offset = self.duration / (float)self.numberPoints;
    
    for(NSUInteger i = 0; i < self.numberPoints; ++i){
        UIView *pointView = [self createPointView];
        [pointsViews addObject:pointView];
        
        [self.animatedView addSubview:pointView];
        
        CAKeyframeAnimation *animation = [self createAnimation];
        animation.timeOffset = offset * i;
        [pointView.layer addAnimation:animation forKey:@"JTBorderDotAnimation"];
    }
}

- (void)stop
{
    for(UIView *pointview in pointsViews){
        [pointview removeFromSuperview];
    }
    [pointsViews removeAllObjects];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)finished
{
    if(finished){
        [self stop];
    }
}

- (UIView *)createPointView
{
    UIView *pointView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.pointSize, self.pointSize)];
    pointView.layer.cornerRadius = self.pointSize / 2.;
    pointView.backgroundColor = self.pointColor;
    
    return pointView;
}

- (CAKeyframeAnimation *)createAnimation
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    animation.duration = self.duration;
    animation.calculationMode = kCAAnimationPaced;
    animation.repeatCount = self.repeatCount;
    animation.delegate = self;
    
    CGFloat animatedWidth = CGRectGetWidth(self.animatedView.frame);
    CGFloat animatedHeight = CGRectGetHeight(self.animatedView.frame);
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL , 0, 0);
    CGPathAddLineToPoint(path, NULL, animatedWidth, 0);
    CGPathAddLineToPoint(path, NULL, animatedWidth, animatedHeight);
    CGPathAddLineToPoint(path, NULL, 0, animatedHeight);
    CGPathAddLineToPoint(path, NULL, 0, 0);
    
    animation.path = path;
    CGPathRelease(path);
    
    return animation;
}

@end


JTBorderDotAnimation.zip (109.42 KB, 下载次数: 288)
462 0 0
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542


代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表