Swift基础之显示波纹样式
最近项目用到了蓝牙连接,搜索设备的内容,其中需要搜索过程中出现波纹的动画效果,在这里将项目中OC语言编写的这种动画效果,转换成Swift编写,下面简单介绍说明一下代码。
这里用到了两种方法实现波纹效果,一种是波纹持续显示,一种是点击以后出现一次波纹的效果
首先是第一种,持续显示波纹
这个内容是重写绘图方法,override func drawRect(rect: CGRect){ }
首先需要设置显示波纹的数量,然后运用for循环进行创建,动画效果,并且添加到layer上
let pulsingCountT:NSInteger = 4;
let animationDurationN:Double = 4;
let animationLayerR = CALayer.init();
for i in 0 ..< pulsingCountT {
let pulsingLayer = CALayer.init();
pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
pulsingLayer.borderColor = UIColor.redColor().CGColor;
pulsingLayer.borderWidth = 1;
pulsingLayer.cornerRadius = rect.size.height/2;
let defaultCurveE = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionDefault);
let animationGroupP = CAAnimationGroup.init();
animationGroupP.fillMode = kCAFillModeBackwards;
animationGroupP.beginTime = CACurrentMediaTime() + Double(i) * animationDurationN / Double(pulsingCountT);
animationGroupP.duration = animationDurationN;
animationGroupP.repeatCount = HUGE;
animationGroupP.timingFunction = defaultCurveE;
let scaleAnimationN = CABasicAnimation.init(keyPath: "transform.scale");
scaleAnimationN.fromValue = 1.4;
scaleAnimationN.toValue = 2.2;
let opacityAnimationN = CAKeyframeAnimation.init(keyPath: "opacity");
opacityAnimationN.values = [1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0];
opacityAnimationN.keyTimes = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1];
animationGroupP.animations = [scaleAnimationN,opacityAnimationN];
pulsingLayer.addAnimation(animationGroupP, forKey: "plulsing");
animationLayerR.addSublayer(pulsingLayer);
}
self.layer.addSublayer(animationLayerR);
绘图完成后,进行添加
//圈视图
circleV = CircleView.init();
circleV.frame = CGRectMake(100, 60, 100, 100);
circleV.hidden = true;
circleV.backgroundColor = UIColor.clearColor();
self.view.addSubview(circleV);
//类方法的调用,只能类本事调用,与OC中的类方法(加号)形式一样
CircleView.showLogStr();
//点击出现圈视图,第一种持续扩散的圈
let button1 = UIButton.init(frame: CGRectMake(100, 60, 100, 100));
button1.setTitle("开启搜索", forState: .Normal);
button1.backgroundColor = UIColor.yellowColor();
button1.layer.cornerRadius = 50;
button1.clipsToBounds = true;
button1.setTitleColor(UIColor.blueColor(), forState: .Normal);
button1.addTarget(self, action: #selector(button1Click), forControlEvents: .TouchUpInside);
self.view.addSubview(button1);
//按钮的点击事件
func button1Click(btn:UIButton) {
circleV.hidden = false;
}
然后是实现点击出现一次波纹的效果
这里用到了类似于OC中类别category文件的实现,使用extension创建类似于类别文件:extension UIView { }
//创建可点击出现的圆圈方法,参数一:表示圈的颜色,参数二:表示圈相对于View扩散的比例大小
func showCircleAnimationLayerWithColor(circleColor:UIColor,andScale aScale:CGFloat){
if (self.superview == false) && (circleColor == true) {
return;
}
let pathFrameE = CGRectMake(-CGRectGetMidX(self.bounds), -CGRectGetMidY(self.bounds), self.bounds.size.width, self.bounds.size.height);
let pathH = UIBezierPath.init(roundedRect: pathFrameE, cornerRadius: self.layer.cornerRadius);
let shapePositionN = self.superview?.convertPoint(self.center, fromView: self.superview);
//内圈1
let circleShapeE1 = CAShapeLayer.init();
circleShapeE1.path = pathH.CGPath;
circleShapeE1.position = shapePositionN!;
circleShapeE1.fillColor = UIColor.clearColor().CGColor;
//不透明
circleShapeE1.opacity = 0;
circleShapeE1.strokeColor = circleColor.CGColor;
circleShapeE1.lineWidth = 0.6;
self.superview?.layer.addSublayer(circleShapeE1);
let scaleAnimationN1 = CABasicAnimation.init(keyPath: "transform.scale");
scaleAnimationN1.fromValue = NSValue.init(CATransform3D: CATransform3DIdentity);
scaleAnimationN1.toValue = NSValue.init(CATransform3D: CATransform3DMakeScale(aScale, aScale, 1));
scaleAnimationN1.duration = 2;
scaleAnimationN1.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear);
circleShapeE1.addAnimation(scaleAnimationN1, forKey: nil);
let alphaAnimationN1 = CABasicAnimation.init(keyPath: "opacity");
alphaAnimationN1.fromValue = 1;
alphaAnimationN1.toValue = 0;
alphaAnimationN1.duration = 1.8;
alphaAnimationN1.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseOut);
circleShapeE1.addAnimation(alphaAnimationN1, forKey: nil);
//内圈2
let circleShapeE2 = CAShapeLayer.init();
circleShapeE2.path = pathH.CGPath;
circleShapeE2.position = shapePositionN!;
circleShapeE2.fillColor = circleColor.CGColor;
//不透明
circleShapeE2.opacity = 0;
circleShapeE2.strokeColor = UIColor.clearColor().CGColor;
circleShapeE2.lineWidth = 0;
self.superview?.layer.insertSublayer(circleShapeE2, atIndex: 0);
let scaleAnimationN2 = CABasicAnimation.init(keyPath: "transform.scale");
scaleAnimationN2.fromValue = NSValue.init(CATransform3D: CATransform3DIdentity);
scaleAnimationN2.toValue = NSValue.init(CATransform3D: CATransform3DMakeScale(aScale, aScale, 1));
scaleAnimationN2.duration = 2;
scaleAnimationN2.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear);
circleShapeE2.addAnimation(scaleAnimationN2, forKey: nil);
let alphaAnimationN2 = CABasicAnimation.init(keyPath: "opacity");
alphaAnimationN2.fromValue = 0.8;
alphaAnimationN2.toValue = 0;
alphaAnimationN2.duration = 1.7;
alphaAnimationN2.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseOut);
circleShapeE2.addAnimation(alphaAnimationN2, forKey: nil);
//线程
let timeE:dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, 2*Int64(NSEC_PER_SEC));
dispatch_after(timeE, dispatch_get_main_queue()) {
circleShapeE1.removeFromSuperlayer();
circleShapeE2.removeFromSuperlayer();
};
}
方法的调用:
//第二种方式,点击出现一个圈视图,而不是持续
let button2 = UIButton.init(frame: CGRectMake(100, 270, 120, 60));
//根据width与height,1:2的比例创建一个椭圆视图
button2.updateMaskToBounds(button2.bounds);
button2.setTitle("点击", forState: .Normal);
button2.backgroundColor = UIColor.yellowColor();
//button2.layer.cornerRadius = 30;
button2.clipsToBounds = true;
button2.setTitleColor(UIColor.blueColor(), forState: .Normal);
button2.addTarget(self, action: #selector(button2Click), forControlEvents: .TouchUpInside);
self.view.addSubview(button2);
按钮点击方法
func button2Click(btn:UIButton) {
btn.showCircleAnimationLayerWithColor(UIColor.whiteColor(), andScale: 3);
}
效果图:
源代码Demo(还有类方法的使用):http://download.csdn.net/detail/hbblzjy/9631221
Swift基础之显示波纹样式的更多相关文章
- Swift基础语法学习总结(转)
Swift基础语法学习总结 1.基础 1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift使用print和println打印,它的传参是一个泛型 ...
- Swift基础语法学习总结
Swift基础语法学习总结Swift高级语法学习总结Swift语法总结补充(一) 1.基础 1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift ...
- Swift基础之PickerView(时间)选择器
代码讲解:(后面有额外代码讲解) 首页设计UIPickerView的样式设计: leftArray = ["花朵","颜色","形状"]; ...
- swift基础:第六部分:类与对象
http://reactnative.cn/docs/0.24/getting-started.html#content(react Native 开发文档) 互联网这个时代,你松懈一天,就会有很多很 ...
- swift基础:第二部分:函数和闭包
今天本来想利用上午的时间本来打算将swift基础部分学习完的,不巧的是,后台来和我讨论用户评价的接口,讨论过后,商讨出一种可行的方案,十几分钟时间过去了,我拿到将接口介入到已经完成的页面中,完美,终于 ...
- Android项目实战(十四):TextView显示html样式的文字
项目需求: TextView显示一段文字,格式为:(消息个数,不确定)条消息 这段文字中名字和数字的长度是不确定的,还要求名字和数字各自有各自的颜色. 一开始我想的是用(转) SpannableStr ...
- swift -- 基础
swift -- 基础 1.常量和变量 常量: let 变量: var 2.声明常量和变量 常量的声明: let let a = 1 //末尾可以不加分号,等号两边的空格必须对应(同 ...
- iOS Swift 模块练习/swift基础学习
SWIFT项目练习 SWIFT项目练习2 iOS Swift基础知识代码 推荐:Swift学习使用知识代码软件 0.swift中的宏定义(使用方法代替宏) 一.视图 +控件 1.UIImag ...
- Swift基础学习
Swift基础学习 http://c.biancheng.net/cpp/html/2242.html 这个网站最近看了一下,对于基本语法解释概括的相对全面,如同重新练习一遍OC似的,挺全面的,谢谢 ...
随机推荐
- Python入门之装饰器九步学习入门
第一步:最简单的函数,准备附加额外功能 '''示例1: 最简单的函数,表示调用了两次''' def myfunc(): print("myfunc() called.") myfu ...
- VB6工程在Win10系统打开提示MSCOMCTL.OCX无法加载
解决办法: 修改.vbp文件中的 00F8754DA1}#2.1#0; MSCOMCTL.OCX 改为 00F8754DA1}#2.0#0; MSCOMCTL.OCX 中间的2.1 改为 2.0
- php+xdebug+dbgp远程调试(多人)
目录 创建 DBGP 服务 配置 调试 创建 DBGP 服务 到 下载页面,下载 python 版本的 dbgp 到服务器上. 解压后执行 pydbgpproxy. 如果提示找不到 dbgp 模块,则 ...
- h5的localStorage和sessionStorage
今天做了个首页的弹窗,要求是打开时显示弹窗,然后点击关闭按钮时弹窗关闭,然后点击不再显示,之后再刷新就不会有弹窗,总结一下需求. 1.弹窗显示隐藏 这个很容易,我们可以用display:none和di ...
- Spring Boot 参数校验
1.背景介绍 开发过程中,后台的参数校验是必不可少的,所以经常会看到类似下面这样的代码 这样写并没有什么错,还挺工整的,只是看起来不是很优雅而已. 接下来,用Validation来改写这段 2.Spr ...
- 54. Spiral Matrix(中等)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- delphi 给EXE文件增加区段
学习 PE 可执行文件格式,用 delphi 实现给 EXE 文件增加区段 源码下载(StudyPE.zip) unit uStudyPE; interface uses Classes, SysUt ...
- 网络编程练习这些就ok
1,什么是C/S架构? C指的是client(客户端软件),S指的是Server(服务端软件) 一个C/S架构就是,实现服务端软件与客户端软件基于网络通信. 互联网中处处是C/S架构 如123 ...
- [Java笔记]继承
继承只是继承框架,而数据没有继承!. 继承不改变父类数据!
- swing JTable
JTable 实例 import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayo ...