心电图动画

心跳动画

在博客上看到youxianming[http://www.cnblogs.com/YouXianMing/p/5122580.html]上传的心电图动画,不禁模仿了一番。仅供学习,向youxianming致敬。

这里写图片描述

//
//  ViewController.m
//  AnimationTest
//
//  Created by Zhang Runchao on 16/3/11.
//  Copyright © 2016年 test. All rights reserved.
//

#import "ViewController.h"
#import "UIView+SetRect.h"
#import "UIView+AnimationProperty.h"
#import "WxHxD.h"
#import "GCD.h"

@interface ViewController ()
@property(nonatomic,strong) UIView *animationView;
@property(nonatomic,strong) CAShapeLayer *shapeLayer;
@property(nonatomic,strong) GCDTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //  画布
    [self setupAnimationView];
    //心跳
    [self setupHeartBeat];
}
-(void)setupAnimationView{
    self.animationView = [[UIView alloc] initWithFrame:CGRectMake(0, 80,Width , 200)];
    [self.animationView setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:self.animationView];

}

-(void)setupHeartBeat{


    //background layer

    {
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = self.animationView.frame;
        UIBezierPath *path = [self heartBeatBezierPath];
        layer.path = path.CGPath;
        layer.lineWidth = .5f;
        layer.fillColor = [[UIColor clearColor] CGColor];
        layer.strokeColor = [[UIColor redColor] CGColor];
        layer.opacity = .5f;
        layer.position = self.animationView.middlePoint;
        layer.shadowColor = [[UIColor redColor] CGColor];
        [layer setTransform:CATransform3DMakeScale(.65f, .65f, 1)];
        [self.animationView.layer addSublayer:layer];
    }
    {
        self.shapeLayer = [CAShapeLayer layer];
        self.shapeLayer.frame = self.animationView.frame;
        self.shapeLayer.path = [self heartBeatBezierPath].CGPath;
        self.shapeLayer.fillColor = [[UIColor clearColor] CGColor];
        self.shapeLayer.strokeColor = [[UIColor redColor] CGColor];
        self.shapeLayer.strokeEnd = 0;
        self.shapeLayer.lineWidth = 2;
        self.shapeLayer.position = self.animationView.middlePoint;
        self.shapeLayer.shadowColor = [[UIColor redColor] CGColor];
        self.shapeLayer.shadowOpacity = 1;
        self.shapeLayer.shadowRadius = 4.f;
        [self.shapeLayer setTransform:CATransform3DMakeScale(.65f, .65f, 1.f)];
        [self.animationView.layer addSublayer:self.shapeLayer];

        CGFloat MAX = 0.98f;
        CGFloat GAP = 0.02;

        self.timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
        [self.timer event:^{
            CABasicAnimation *aniStart = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
            aniStart.fromValue = [NSNumber numberWithFloat:0.f];
            aniStart.toValue = [NSNumber numberWithFloat:MAX];
            aniStart.duration = 4.9f;

            CABasicAnimation *aniEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
            aniEnd.fromValue = [NSNumber numberWithFloat:0.f+GAP];
            aniEnd.toValue = [NSNumber numberWithFloat:MAX+GAP];
            aniEnd.duration = 4.9f;

            CAAnimationGroup *group = [CAAnimationGroup animation];
            group.duration = 4.9f;
            group.animations = @[aniEnd,aniStart];
            self.shapeLayer.strokeStart   = MAX;
            self.shapeLayer.strokeEnd     = MAX + GAP;
            [self.shapeLayer addAnimation:group forKey:nil];


        } timeIntervalWithSecs:5.f delaySecs:1.f];
        [self.timer start];
    }
}

//生成贝塞尔

-(UIBezierPath*)heartBeatBezierPath{
    UIBezierPath *path = [UIBezierPath bezierPath];
    //画路径
    [path moveToPoint:CGPointMake(0, 100)];
    [path addLineToPoint:CGPointMake(20, 100)];
    [path addLineToPoint:CGPointMake(35, 150)];
    [path addLineToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(65, 185)];
    [path addLineToPoint:CGPointMake(80, 100)];
    [path addLineToPoint:CGPointMake(95, 100)];
    [path addLineToPoint:CGPointMake(110, 20)];
    [path addLineToPoint:CGPointMake(125, 170)];
    [path addLineToPoint:CGPointMake(140, 40)];
    [path addLineToPoint:CGPointMake(155, 100)];
    [path addLineToPoint:CGPointMake(175, 100)];
    [path addLineToPoint:CGPointMake(200, 150)];
    [path addLineToPoint:CGPointMake(215, 10)];
    //线连接处样式
    path.lineCapStyle = kCGLineCapSquare;
    //线终点处样式
    path.lineJoinStyle = kCGLineJoinBevel;
    [[UIColor blackColor] setStroke];
    path.lineWidth = 1;
    //根据坐标点连线
    [path stroke];
    return path;

}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值