iOS 实现加载转圈效果

时间:2022-09-03 09:00:09

1、思路:

新建一个view,添加shape,给予一个动画实现。

2、效果图:

效果1:

iOS 实现加载转圈效果

效果2:

iOS 实现加载转圈效果

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 实现加载转圈效果的更多相关文章

  1. ios 自定义加载动画效果

    在开发过程中,可能会遇到各种不同的场景需要等待加载成功后才能显示数据.以下是自定义的一个动画加载view效果.      在UIViewController的中加载等到效果,如下 - (void)vi ...

  2. iOS - 落叶加载动画效果

    代码下载地址:https://github.com/nLoser/LeafLoadingView 效果: 说明:效果是在网上看到的,并且自己按照效果自己实现,树叶使用CAEmitterLayer做的, ...

  3. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化   在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...

  4. 使用Bootstrap3和Ladda UI实现的多种按钮“加载中”效果体验

    在线演示 在线演示 大家在开发基于web的网站或者web应用中,常常在AJAX调用的过程中需要提示用户并且展示相关的“加载中”效果,类似的UI设计也非常多,比如,当点击一个按钮后,在它的旁边显示一个 ...

  5. iOS图片加载到内存中占用内存情况

    我的测试结果: 图片占用内存   图片尺寸           .png文件大小 1MB              512*512          316KB 4MB              10 ...

  6. jQuery8种不同的瀑布流懒加载loading效果

    优化图片加载插件jQuery8种不同的瀑布流懒加载loading效果  在线预览 下载地址 实例代码 <ul class="grid effect-1" id="g ...

  7. 一个很酷的加载loading效果--IT蓝豹

    一个很酷的加载loading效果,自定义LeafLoadingView实现,LeafLoadingView继承view, 本例子主要由以下几点构成 (1):RotateAnimation实现叶子旋转 ...

  8. Ladda – 把加载提示效果集成到按钮中,提升用户体验

    Ladda 是一组集成了加载提示的按钮,以弥合行动和反馈之间的时间间隔,提供更好的功能使用体验.主要用于在用户点击提交之后,向用户提供即时的反馈,让他们知道浏览器正在处用户提交的任务. 您可能感兴趣的 ...

  9. iOS - &lpar;懒加载&rpar;

    今天很坑爹,做界面的时候,tableview 明显做了复用了,数组也做了懒加载了,获取数据前也把数组给清空了,但是每次获取数据刷新表格的时候,数据确重复覆盖了(重复创建),后来给 cell 加了个白色 ...

随机推荐

  1. Leetcode&colon; Ones and Zeroes

    In the computer world, use restricted resource you have to generate maximum benefit is what we alway ...

  2. 《BI那点儿事》数据挖掘初探

    什么是数据挖掘? 数据挖掘(Data Mining),又称信息发掘(Knowledge Discovery),是用自动或半自动化的方法在数据中找到潜在的,有价值的信息和规则. 数据挖掘技术来源于数据库 ...

  3. requireJs和r&period;js压缩工具

    上面release是执行命令 node r.js -o build.js 生成的,需要切换到目录require/tools下面,也就是 有r.js和build.js的目录,才能执行命令 代码目录如上: ...

  4. Codeforces Round &num;355 &lpar;Div&period; 2&rpar; D&period; Vanya and Treasure dp&plus;分块

    题目链接: http://codeforces.com/contest/677/problem/D 题意: 让你求最短的从start->...->1->...->2->. ...

  5. thymleaf th&colon;if判断某值不为空

    简单描述:判断后台传递过来的值,是否为空,来做一些业务上的处理. 代码: <div class="col-md-6" th:if="${not #strings.i ...

  6. &lpar;转&rpar;关于python3中staticmethod&lpar;静态方法&rpar;classmethod&lpar;类方法&rpar;实例方法的联系和区别

    原文:http://dmcoders.com/2017/08/30/pythonclass/ https://zhuanlan.zhihu.com/p/28010894------正确理解Python ...

  7. 在TQ2440上运行perf,生成Flame Graph

    参考 http://www.cnblogs.com/helloworldtoyou/p/5585152.html  http://blog.csdn.net/mtofum/article/detail ...

  8. golang闭包里的坑

    介绍 go的闭包是一个很有用的东西.但是如果你不了解闭包是如何工作的,那么他也会给你带来一堆的bug.这里我会拿出Go In Action这本书的一部分代码,来说一说在使用闭包的时候可能遇到的坑.全部 ...

  9. Gulp的安装配置过程和一些小坑

    谈谈gulp. 项目尾声,老师叫我去熟悉一下grunt前端自动化工具,第一次知道这种东西,我就查各种资料啊,发现grunt已经‘过时’了,大家都用gulp和webpack.我当然选择了配置最简单的gu ...

  10. 最近遇到的几个纯C编程的陷阱

    首先是一个不容易看出来的语法上的陷阱 经过调试得出的错误是对非socket的socket操作出错,sockfd在调试过程中发现是0,不是一个合理的文件描述符. 仔细一看原来是括号忘记加了,该运算是先用 ...