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

本文共 3034 字,大约阅读时间需要 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

核心

转载地址:http://gbdux.baihongyu.com/

你可能感兴趣的文章
WebApp之Meta标签
查看>>
添加Java文档注释
查看>>
Python3批量爬取网页图片
查看>>
iphone-common-codes-ccteam源代码 CCEncoding.m
查看>>
微信公众平台开发(96) 多个功能整合
查看>>
[转]MVC4项目中验证用户登录一个特性就搞定
查看>>
用Perl编写Apache模块续二 - SVN动态鉴权实现SVNAuth 禅道版
查看>>
Android 阴影,圆形的Button
查看>>
C++概述
查看>>
卡特兰数
查看>>
006_mac osx 应用跨屏幕
查看>>
nginx中配置文件的讲解
查看>>
MindNode使用
查看>>
SQL Server 2016 Alwayson新增功能
查看>>
HTTP库Axios
查看>>
CentOS7下安装python-pip
查看>>
认知计算 Cognitive Computing
查看>>
左手坐标系和右手坐标系 ZZ
查看>>
陀螺仪主要性能指标
查看>>
Java 架构师眼中的 HTTP 协议
查看>>