iOS 实现加载转圈效果
1、思路:
新建一个view,添加shape,给予一个动画实现。
2、效果图:
效果1:

效果2:

gif有点卡,代码运行不会这样。
3、源码(整个类放进来的)
效果1源码:
//
// YJDownloadingCircle.swift
// k12_sl_iOS
//
// Created by liyajun on 2017/7/13.
//
// import UIKit class YJDownloadingCircle: UIView { var loadingLayer:CAShapeLayer! = nil override init(frame: CGRect) {
super.init(frame: frame) initViews()
} required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initViews() } override func awakeFromNib() {
initViews()
} func initViews() {
backgroundColor = UIColor.white
} func drawHalfCircle() {
loadingLayer = self.drawCircle() loadingLayer.strokeStart = 0.0
loadingLayer.strokeEnd = 0.75 let basicAni = CABasicAnimation(keyPath: "transform.rotation.z")
basicAni.fromValue = 0.0
basicAni.toValue = M_PI*
basicAni.duration = 0.5
basicAni.repeatCount = MAXFLOAT
basicAni.autoreverses = false
basicAni.fillMode = kCAFillModeForwards
self.layer.add(basicAni, forKey: nil) } private func drawCircle() -> CAShapeLayer { let circleLayer = CAShapeLayer()
let rect = CGRect(x: , y: , width: self.frame.size.width, height: self.frame.size.height)
circleLayer.frame = rect
circleLayer.position = CGPoint(x: self.frame.size.width/, y: self.frame.size.height/)
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.lineWidth =
circleLayer.strokeColor = UIColor.colorWithHex(hex: "FF3B30").cgColor
let bezier = UIBezierPath(ovalIn: rect)
circleLayer.path = bezier.cgPath
self.layer.addSublayer(circleLayer) return circleLayer }
}
使用方法
//定义属性
var circle:YJDownloadingCircle! = nil
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
circle = YJDownloadingCircle(frame: CGRect(x: 100, y: 100, width: 36, height: 36))
circle.drawHalfCircle()
self.view.addSubview(circle)
}
效果2源码:
//
// LoginLoadingView.swift
// k12_sl_iOS
//
// Created by liyajun on 2018/5/30.
// import UIKit class LoginLoadingView: UIView { var bgShape:CAShapeLayer! = nil
var runShape:CAShapeLayer! = nil
var lblShape:CAShapeLayer! = nil let startAngle:CGFloat = ;
let endAngle:CGFloat = CGFloat(0.67 * M_PI);
let lineWidth:CGFloat = ; override init(frame: CGRect) {
super.init(frame: frame) setup()
} required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
} func setup() {
bgShape = CAShapeLayer()
let bgPath = CGMutablePath()
bgPath.addEllipse(in: CGRect(x: , y: , width: self.width, height: self.height))
bgShape.fillColor = UIColor.clear.cgColor
bgShape.strokeColor = UIColor.colorWithHex("4CD964").withAlphaComponent(0.5).cgColor
bgShape.lineWidth = lineWidth
bgShape.lineJoin = kCALineJoinRound
bgShape.lineDashPattern = [NSNumber(value: ),NSNumber(value:)]
bgShape.path = bgPath
self.layer.addSublayer(bgShape) runShape = CAShapeLayer()
let runPath = UIBezierPath(arcCenter: CGPoint(x: , y: ), radius: self.width/, startAngle: startAngle, endAngle: endAngle, clockwise: false)
runShape.fillColor = UIColor.clear.cgColor
runShape.strokeColor = UIColor.colorWithHex("4CD964").cgColor
runShape.lineWidth = lineWidth
runShape.lineJoin = kCALineJoinRound
runShape.path = runPath.cgPath
runShape.position = CGPoint(x: self.width/, y: self.width/)
self.layer.addSublayer(runShape) let anima = CABasicAnimation(keyPath: "transform.rotation.z")
anima.fromValue =
anima.toValue = M_PI*
anima.repeatCount = MAXFLOAT
anima.duration = 2.0
anima.isRemovedOnCompletion = false
runShape.add(anima, forKey: "rotationAnnimation") lblShape = CAShapeLayer()
let gouPath = UIBezierPath()
gouPath.move(to: CGPoint(x: self.width/-, y: self.width/-))
gouPath.addLine(to: CGPoint(x: self.width/, y: self.width/+))
gouPath.addLine(to: CGPoint(x: self.width/+, y: self.width/-))
lblShape.fillColor = UIColor.clear.cgColor
lblShape.strokeColor = UIColor.colorWithHex("4CD964").cgColor
lblShape.lineWidth = lineWidth
lblShape.lineJoin = kCALineJoinRound
lblShape.path = gouPath.cgPath
self.layer.addSublayer(lblShape)
} }
使用时,直接init出来就是了
以上的效果都是只有一个类文件(UIView),使用时直接拷贝即可。
其中,关于转圈的前景色、背景色等参数,我没抽出来,大家如果有这需求,可以改一下。
代码是Swift实现加载转圈效果。
如果是OC,参考代码思路即可。
enjoy~
iOS 实现加载转圈效果的更多相关文章
- ios 自定义加载动画效果
在开发过程中,可能会遇到各种不同的场景需要等待加载成功后才能显示数据.以下是自定义的一个动画加载view效果. 在UIViewController的中加载等到效果,如下 - (void)vi ...
- iOS - 落叶加载动画效果
代码下载地址:https://github.com/nLoser/LeafLoadingView 效果: 说明:效果是在网上看到的,并且自己按照效果自己实现,树叶使用CAEmitterLayer做的, ...
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...
- 使用Bootstrap3和Ladda UI实现的多种按钮“加载中”效果体验
在线演示 在线演示 大家在开发基于web的网站或者web应用中,常常在AJAX调用的过程中需要提示用户并且展示相关的“加载中”效果,类似的UI设计也非常多,比如,当点击一个按钮后,在它的旁边显示一个 ...
- iOS图片加载到内存中占用内存情况
我的测试结果: 图片占用内存 图片尺寸 .png文件大小 1MB 512*512 316KB 4MB 10 ...
- jQuery8种不同的瀑布流懒加载loading效果
优化图片加载插件jQuery8种不同的瀑布流懒加载loading效果 在线预览 下载地址 实例代码 <ul class="grid effect-1" id="g ...
- 一个很酷的加载loading效果--IT蓝豹
一个很酷的加载loading效果,自定义LeafLoadingView实现,LeafLoadingView继承view, 本例子主要由以下几点构成 (1):RotateAnimation实现叶子旋转 ...
- Ladda – 把加载提示效果集成到按钮中,提升用户体验
Ladda 是一组集成了加载提示的按钮,以弥合行动和反馈之间的时间间隔,提供更好的功能使用体验.主要用于在用户点击提交之后,向用户提供即时的反馈,让他们知道浏览器正在处用户提交的任务. 您可能感兴趣的 ...
- iOS - (懒加载)
今天很坑爹,做界面的时候,tableview 明显做了复用了,数组也做了懒加载了,获取数据前也把数组给清空了,但是每次获取数据刷新表格的时候,数据确重复覆盖了(重复创建),后来给 cell 加了个白色 ...
随机推荐
- [leetcode]Unique Binary Search Trees @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- 集合 enum 枚举 简介 案例 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 初识C#程序结构
一.编写第一个C#程序.注释: 1.编写程序 static void Main(string[] args)//在Mian方法下编写代码输出数据 { C ...
- 重装linuxserver简易流程
项目开发软件开发非常重要的一个环节,而能够拥有一个安全健康的server来使System正常高效的执行也是非常有必要的.由于是搭建在外网上的server.时不时会受到各种病毒的侵袭, ...
- 建模角度理解word embedding及tensorflow实现
http://www.jianshu.com/p/d44ce1e3ec2f 1. 前言 本篇主要介绍关键词的向量表示,也就是大家熟悉的word embedding.自Google 2013 年开源wo ...
- ESXI 6.5 从载到安装
下载ESXI 访问官网 https://www.vmware.com 到download,选择vsphere 下载esxi https://my.vmware.com/en/group/vmware/ ...
- Objective-C编程 - 关于Block的要点
1. 首先,我们快速过一下,什么是Block? Block是一段代码,它在OC中以^开头,可以有返回值,和参数列表,但就是没有名字. 所以,你可以把它认为是匿名函数. 事实上,它和Swift中的闭包( ...
- Windows 10 中的 Shell 指令
这篇文章本无技术含量,也不高大上,不过想想,还是写下来吧,肯定有人会用得到的. 常玩系统的朋友,应该知道 Shell 指令,这是一个很好玩很神奇的指令.利用它,可以用简短的单词,轻易地打开系统中的一些 ...
- Dicom格式文件解析器[转]
Dicom格式文件解析器 Dicom全称是医学数字图像与通讯,这里讲的暂不涉及通讯那方面的问题 只讲*.dcm 也就是diocm格式文件的读取,读取本身是没啥难度的 无非就是字节码数据流处理.只不 ...
- win7下安装matlab后打开出错“error starting desktop”的解决办法
在matlab快捷图标上右键,选择"还原以前的版本"--"兼容性"选项卡,在"以兼容模式运行这个程序"前面打勾,并选择"windo ...