博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用path动画绘制水波纹
阅读量:5058 次
发布时间:2019-06-12

本文共 3089 字,大约阅读时间需要 10 分钟。

用path动画绘制水波纹

 

效果

 

源码

////  ViewController.m//  PathAnimation////  Created by YouXianMing on 15/7/3.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong)  CAShapeLayer   *animationLayer;@property (nonatomic, strong)  NSTimer        *timer;@property (nonatomic)          CGPathRef       oldPath;@property (nonatomic)          CGPathRef       path;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        self.animationLayer               = [CAShapeLayer layer];    self.animationLayer.borderWidth   = 0.5f;    self.animationLayer.frame         = CGRectMake(0, 0, 200, 200);    self.animationLayer.position      = self.view.center;    self.animationLayer.path          = [self createPath].CGPath;    self.animationLayer.fillColor     = [UIColor redColor].CGColor;    [self.view.layer addSublayer:self.animationLayer];    _timer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(event) userInfo:nil repeats:YES];}- (void)event {        _oldPath = self.animationLayer.path;    _path    = [self createPath].CGPath;        CABasicAnimation *basicAnimation   = [CABasicAnimation animationWithKeyPath:@"path"];    basicAnimation.duration            = 0.5;    basicAnimation.fromValue           = (__bridge id)(_oldPath);    basicAnimation.toValue             = (__bridge id)_path;    self.animationLayer.path           = _path;        [self.animationLayer addAnimation:basicAnimation forKey:@"animateCirclePath"];}- (UIBezierPath *)createPath {        static int count = 0;        CGFloat controlPoint1_X = 0;    CGFloat controlPoint1_Y = 0;    CGFloat controlPoint2_X = 0;    CGFloat controlPoint2_Y = 0;        if (count ++ % 2 == 0) {                controlPoint1_X = [self randomNum_70_79];        controlPoint1_Y = [self randomNum_70_79];                controlPoint2_X = [self randomNum_120_129];        controlPoint2_Y = [self randomNum_120_129];            } else {            controlPoint1_X = [self randomNum_70_79];        controlPoint1_Y = [self randomNum_120_129];                controlPoint2_X = [self randomNum_120_129];        controlPoint2_Y = [self randomNum_70_79];            }        // 获取贝塞尔曲线    UIBezierPath* bezierPath = [UIBezierPath bezierPath];        // A    [bezierPath moveToPoint:CGPointMake(0, 100)];        // B (Curve)    [bezierPath addCurveToPoint:CGPointMake(200, 100)                  controlPoint1:CGPointMake(controlPoint1_X, controlPoint1_Y)                  controlPoint2:CGPointMake(controlPoint2_X, controlPoint2_Y)];        // C    [bezierPath addLineToPoint:CGPointMake(200, 200)];        // D    [bezierPath addLineToPoint:CGPointMake(0, 200)];        // 闭合曲线    [bezierPath closePath];        return bezierPath;}/** *  随机数 70 - 79 * *  @return 随机数 */- (CGFloat)randomNum_70_79 {    return (CGFloat)(arc4random() % 10 + 70);}/** *  随机数 120 - 129 * *  @return 随机数 */- (CGFloat)randomNum_120_129 {    return (CGFloat)(arc4random() % 10 + 120);}@end

 

核心

 

转载于:https://www.cnblogs.com/YouXianMing/p/4619746.html

你可能感兴趣的文章
Maven启用代理访问
查看>>
Primary definition
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
发送请求时params和data的区别
查看>>
JavaScript 克隆数组
查看>>
eggs
查看>>
一步步学习微软InfoPath2010和SP2010--第七章节--从SP列表和业务数据连接接收数据(4)--外部项目选取器和业务数据连接...
查看>>
如何增强你的SharePoint 团队网站首页
查看>>
FZU 1914 Funny Positive Sequence(线性算法)
查看>>
oracle 报错ORA-12514: TNS:listener does not currently know of service requested in connec
查看>>
基于grunt构建的前端集成开发环境
查看>>
MySQL服务读取参数文件my.cnf的规律研究探索
查看>>
java string(转)
查看>>
__all__有趣的属性
查看>>
BZOJ 5180 [Baltic2016]Cities(斯坦纳树)
查看>>
写博客
查看>>
利用循环播放dataurl的视频来防止锁屏:NoSleep.js
查看>>
python3 生成器与迭代器
查看>>
java编写提升性能的代码
查看>>