CAShapeLayer和UIBezierPath的应用

公司项目中有一个部分,需要做一个类似水波纹的动画,我想到的方法是CAShapeLayer和UIBezierPath

CAShapeLayer和UIBezierPath的应用_第1张图片
动画效果
CAShapeLayer和UIBezierPath的应用_第2张图片
动画效果

其实原理就是CAShapeLayer 加在按钮下方,然后每隔一段时间间隔添加一次,且每次layer做透明度和路径的修改动画即可

代码如下
ViewController.m

//
//  ViewController.m
//  TestPractise
//
//  Created by 李昭宏 on 2017/1/13.
//  Copyright © 2017年 scut. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIButton *button;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) CAShapeLayer *rippleLayer;

@end

static const CGFloat kButtonWidthHeight = 80.f;
static const CGFloat kButtonTopPadding  = 50.f;
static const CGFloat kAnimationDuration = 1.5f;
static const NSInteger kAnimationCount  = 3;

@implementation ViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.view.backgroundColor = [UIColor yellowColor];
    [self configView];
    [self startAnimation];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private method

- (void)configView {
    [self.view addSubview:self.button];
}

- (void)startAnimation {
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    [self.timer fire];
}

- (void)addShapeLayer {
    CAShapeLayer *rippleLayer = [[CAShapeLayer alloc] init];
    self.rippleLayer = rippleLayer;
//    rippleLayer.bounds = self.button.bounds;
//    rippleLayer.position = self.button.center;
    //配置layer的大小和填充色
    rippleLayer.frame = self.button.frame;
    rippleLayer.backgroundColor = [UIColor clearColor].CGColor;
    rippleLayer.fillColor = [UIColor purpleColor].CGColor;
    
    //配置起始的终止路径
    CGRect originRect = CGRectMake(5, 5, CGRectGetWidth(self.button.frame) - 10, CGRectGetHeight(self.button.frame) - 10);
    UIBezierPath *beginPath = [UIBezierPath bezierPathWithOvalInRect:originRect];
    CGRect endRect = CGRectMake(-40, -40, CGRectGetWidth(self.button.frame) + 80, CGRectGetHeight(self.button.frame) + 80);
    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:endRect];
    
    //路径动画
    CABasicAnimation *rippleAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    rippleAnimation.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
    rippleAnimation.toValue = (__bridge id _Nullable)(endPath.CGPath);
    rippleAnimation.duration = kAnimationDuration;
   
    //透明度动画
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = [NSNumber numberWithFloat:0.4];
    opacityAnimation.toValue = [NSNumber numberWithFloat:0.0];
    opacityAnimation.duration = kAnimationDuration;
    
    [rippleLayer addAnimation:opacityAnimation forKey:@""];
    [rippleLayer addAnimation:rippleAnimation forKey:@""];
    
    [self.view.layer insertSublayer:rippleLayer below:self.button.layer];
}

#pragma mark - getter

- (NSTimer *)timer {
    if(_timer) {
        return _timer;
    }
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:kAnimationDuration / kAnimationCount target:self selector:@selector(addShapeLayer) userInfo:nil repeats:YES];
    return _timer;
}

- (UIButton *)button {
    if(_button) {
        return _button;
    }
    
    _button = [[UIButton alloc] initWithFrame:CGRectMake((CGRectGetWidth([UIScreen mainScreen].bounds) - kButtonWidthHeight) / 2, kButtonTopPadding, kButtonWidthHeight, kButtonWidthHeight)];
    [_button setTitle:@"Hello" forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    _button.titleLabel.font = [UIFont systemFontOfSize:14];
    _button.backgroundColor = [UIColor whiteColor];
    _button.layer.cornerRadius = CGRectGetWidth(_button.frame) / 2;
    return _button;
}


@end

这大概算是CALayer的简单使用,之后有更多关于CALayer和UIBezierPath的效果,会拿出来给大家看

你可能感兴趣的:(CAShapeLayer和UIBezierPath的应用)