import UIKit

class CVLayerView: UIView {

var pulseLayer : CAShapeLayer!  //定义图层

override init(frame: CGRect) {

super.init(frame: frame)

let width = self.bounds.size.width

// 动画图层

pulseLayer = CAShapeLayer()

pulseLayer.bounds = CGRect(x: 0, y: 0, width: width, height: width)

pulseLayer.position = CGPoint(x: width/2, y: width/2)

pulseLayer.backgroundColor = UIColor.clear.cgColor

// 用BezierPath画一个原型

pulseLayer.path = UIBezierPath(ovalIn: pulseLayer.bounds).cgPath

// 脉冲效果的颜色  (注释*1)

pulseLayer.fillColor = UIColor.init(r: 213, g: 54, b: 13).cgColor

pulseLayer.opacity = 0.0

// 关键代码

let replicatorLayer = CAReplicatorLayer()

replicatorLayer.bounds = CGRect(x: 0, y: 0, width: width, height: width)

replicatorLayer.position = CGPoint(x: width/2, y: width/2)

replicatorLayer.instanceCount = 3  // 三个复制图层

replicatorLayer.instanceDelay = 1  // 频率

replicatorLayer.addSublayer(pulseLayer)

self.layer.addSublayer(replicatorLayer)

self.layer.insertSublayer(replicatorLayer, at: 0)

}

func starAnimation() {

// 透明

let opacityAnimation = CABasicAnimation(keyPath: "opacity")

opacityAnimation.fromValue = 1.0  // 起始值

opacityAnimation.toValue = 0     // 结束值

// 扩散动画

let scaleAnimation = CABasicAnimation(keyPath: "transform")

let t = CATransform3DIdentity

scaleAnimation.fromValue = NSValue(caTransform3D: CATransform3DScale(t, 0.0, 0.0, 0.0))

scaleAnimation.toValue = NSValue(caTransform3D: CATransform3DScale(t, 1.0, 1.0, 0.0))

// 给CAShapeLayer添加组合动画

let groupAnimation = CAAnimationGroup()

groupAnimation.animations = [opacityAnimation,scaleAnimation]

groupAnimation.duration = 3   //持续时间

groupAnimation.autoreverses = false //循环效果

groupAnimation.repeatCount = HUGE

groupAnimation.isRemovedOnCompletion = false

pulseLayer.add(groupAnimation, forKey: nil)

}

required init?(coder aDecoder: NSCoder) {

super.init(coder: aDecoder)

}

}

使用方法

let waveView = CVLayerView(frame: CGRect(x: margin*CGFloat(index)*2+margin/2, y: 10, width: margin, height: margin))

waveView.center = button.center

// 因为我的VC是XIB  所以将图层添加在到按钮之下 一般用法就直接addSubview就可以了

//                self.insertSubview(waveView, belowSubview: button)

self.addSubview(waveView)

waveView.starAnimation()  // 开始动画

swift之水纹动画的更多相关文章

  1. Swift - transform.m34动画示例

    Swift - transform.m34动画示例 效果 源码 https://github.com/YouXianMing/Swift-Animations // // CATransform3DM ...

  2. Swift 表视图动画教程: Drop-In Cards

    http://www.raywenderlich.com/76024/swift-table-view-animations-tutorial-drop-cards 标准 table view 是一个 ...

  3. swift - 歌曲列表动画

    // //  ViewController.swift //  songAnimation // //  Created by su on 15/12/10. //  Copyright © 2015 ...

  4. iOS开发——动画篇Swift篇&常用动画总结

    UIView动画: UIView动画时最基本的动画,是直接对我们界面上控件进行简单的动画效果实现,如果你只需要用到一些简单的效果,那么这个很适合你,关于UIView动画实现恨简单, UIKit直接将动 ...

  5. swift 之SnapKit 动画

    这个问题纠结了我挺长时间的.一直以为把约束直接添加到动画里面就可以了.但是并没那么简单.-.-其实还是挺简 class ViewController: UIViewController { @IBOu ...

  6. swift 头尾式动画

    1.0 头尾式动画 UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(1.0) // 设置执行动画所需要的时间 ...

  7. swift 设置图片动画组 iOS11之前 默认图片 设置不成功

    在iOS 11 上, 1.先执行动画组 在设置图片执行帧动画,2.先设置图片在设置帧动画,执行帧动画  没有任何问题 在iOS 10和iOS9上,必须 执行 方法二(先设置图片在设置帧动画,执行帧动画 ...

  8. 你会用swift创建复杂的加载动画吗(1)

    时至今日,iOS 应用商店已经拥有超过了140万 应用,让你自己的应用脱颖而出确实是个不小的挑战.不过,在你的应用掉入默默无闻的大黑洞之前,你拥有一个小小的机遇窗,它能帮你吸引用户的注意. AD: 时 ...

  9. iOS - 开源框架、项目和学习资料汇总(动画篇)

    动画 1. Core Animation笔记,基本的使用方法 – Core Animation笔记,基本的使用方法:1.基本动画,2.多步动画,3.沿路径的动画,4.时间函数,5.动画组.2. awe ...

随机推荐

  1. 生成MyEclipse6.5&7.5&8.5 注册机源码

    分类: java技术2010-09-30 21:46 26638人阅读 评论(6) 收藏 举报 myeclipsejavastringimportinputbyte 生成MyEclipse8.5注册码 ...

  2. 题解 nflsoj550 【六校联合训练 省选 #9】序列

    题目链接 以下把值域(题面里的\(lim\))记做\(m\). 考虑求\(k\)的答案.考虑每个位置对答案的贡献,枚举位置\(i\),再枚举\(a[i]\)的值\(x\).设: \[ F(k)=\su ...

  3. firewalld学习--service的使用和配置

    service配置文件 firewalld默认给我们提供的ftp的service配置文件ftp.xml <?xml version="1.0" encoding=" ...

  4. maven详解 之 pom.xml

    Maven  一个项目管理工具  其作用就是用来管理jar 包的 maven的核心    pom.xml配置文件 <project xmlns="http://maven.apache ...

  5. 微信扫码登陆(JAVA)

    在web端用到weChat扫码登录,在手机扫码登陆成功后,跳转到相应的界面. 1.第一步请求code 调用接口:https://open.weixin.qq.com/connect/qrconnect ...

  6. openalyser6学习

    1.安装nvm.nvm下载地址:https://github.com/coreybutler/nvm-windows/releases使用nvm-setup安装和下载nodejs:https://ww ...

  7. dedecms 标签使用 runphp=php 获取文章静态地址

    [field:id runphp='yes'] $url=GetOneArchive(@me); @me=$url['arcurl']; [/field:id]

  8. Linux每日练习-awk命令之内部自定义函数 20200224

  9. html5游戏的横屏问题

    html5 API有这个参数 Screen Orientation API 可以看w3c定义的规范 The Screen Orientation API <!-- UC强制竖屏 --> & ...

  10. C++面试常见问题——06数组排序

    数组排序 冒泡.最简单的冒泡,没啥好讲的 #include<iostream> using namespace std; void BubbleSort(int a[],int len){ ...