心跳动画
在博客上看到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