先来看下效果

下拉刷新

其实下拉刷新没大家想得那么难。本文已第二个为例子。给大家讲解下下拉刷新的做法(完整代码后面会放上)

首先,先搞一个single View Application 。然后进Main.storyboard中,选中viewController

t1.png

按照图中方法,加一个导航。 然后然后拖一个tableview到viewController上。设置下四边的约束都为0 。
选中tableview 把有边框中Prototype Cells 设置为1 。选中Cell右边框中Identifier 设置为Cell
,然后再设置下代理

t2.png

这些都是些基础的操作,不再赘述了。
然后代码中也是很基础的 搞出点数据就行了

import UIKit

class SecondViewController: UIViewController,UITableViewDataSource,UITableViewDelegate , RefreshDelegate{

    @IBOutlet weak var tableView:UITableView!

    var datas = ["第一行","第二行","第三行","第四行","第五行"]

    override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath);
cell.textLabel?.text = datas[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count;
}
}

下来就该自定义我们的刷新了。。

下拉刷新,能下拉肯定是scrollview 。所以我们需要scrollview的一些方法。这里我们的view继承自UIView ,实现
UIScrollViewDelegate协议

class DZRefreshViewTwo: UIView,UIScrollViewDelegate {

首先我们定义一个scrollview 作为成员变量

var scrollView:UIScrollView!

由于我们这个view是放在一个scrollview中的 所以我们初始化的时候需要传入一个scrollview来作为我们view的父容器

    init(frame: CGRect , scrollView:UIScrollView) {
super.init(frame: frame)
self.scrollView = scrollView
scrollView.delegate = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
convenience init(scrollView:UIScrollView){
if let sv = scrollView.superview {
self.init(frame:CGRectMake(0,-80, sv.frame.size.width ,80),scrollView:scrollView)
}else{
self.init(frame:CGRectMake(0,-80, scrollView.frame.size.width ,80),scrollView:scrollView)
}
}

这里我们添加了几个构造方法 , 用户可以自己制定frame , 用户如果不想自己指定我们还提供了默认的frame 。这里为啥要判断 if let sv = scrollView.superview 有没有父容器呢?因为我们这个scrollview有可能是通过约束放上去得 frame属性就不准了 ,用它父类的比较准确 。

这里80是我们设定的默认宽度,Y是-80就是初始是看不到的。只有下拉才能看到。所以下拉的过程中这个view要慢慢出现。这个view是放在 scrollview中的。所以只需要操作scrollview即可,这里需要使用scrollview计算下拉的进度,把这个height(80)全部 拉出来的时候就是1

先声明一个progress的成员变量var progress:CGFloat = 0.0 然后实现scrollview协议的下面方法

   //计算进度
func scrollViewDidScroll(scrollView: UIScrollView) {
let offY = max(-1*(scrollView.contentOffset.y+scrollView.contentInset.top),0)
progress = min(offY / self.frame.size.height , 1.0)
}

这里就是计算了拉下来的长度与height的比 ,只有contentOffset和contentInset是什么意思, 网上查查 这些都是scrollview的基础。不再本例子范围内

我们例子中 在下拉的同时,有一个灰色一坨被拉出来(哈哈--粗鲁了。)

所以每次计算完进度后要进行绘制。绘制图形 是CAShapeLayer的强项,这里我们先声明一个CAShapeLayer的成员常量

    var shapeLayer = CAShapeLayer()

然后在初始化的时候给出边的颜色并添加到view的layer上

 shapeLayer.strokeColor = UIColor.grayColor().CGColor
layer.addSublayer(shapeLayer)

其他的属性,要在下拉的时候绘制,添加绘制的方法

 //绘制
func reDraw(offY:CGFloat){
shapeLayer.fillColor = UIColor.grayColor().CGColor
let y = frame.size.height - offY + 1
let width = frame.size.width * progress * 0.8 > 15 ? 15:frame.size.width * progress * 0.8
shapeLayer.path = UIBezierPath(ovalInRect: CGRectMake((frame.size.width/2) - 7.5 , y , width , frame.size.height * progress * 0.8)).CGPath }

这里指定填充颜色 , 利用UIBezierPath(ovalInRect:CGRect) 方法,其实就是在一个矩形中画圆或者椭圆。我们只需要控制矩形的位置和长宽就行了 , 这里高度和长宽都会随下拉变化 。 具体度大家也可以自己把握。也可以按照我上面设置的 。

然后把这个方法添加到func scrollViewDidScroll(scrollView: UIScrollView)的最后面

我们到现在只做了下拉过程中的部分 , 下拉结束呢 ,下拉结束的时候会调用scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 我们在这个方法中进行刷新操作,开始刷新,既然开始刷新要判断是否正在刷新。 所以要先声明一个变量var isRefreshing = false

而且具体的刷新操作肯定不是放在本类执行 ,是要给使用此刷新控件的view执行,这里用一个代理方法

先声明一个协议

protocol RefreshDelegate{
func doRefresh(refreshView: DZRefreshViewTwo)
}

然后在本类声明这个代理的变量var delegate:RefreshDelegate?

在本类中添加一个开始动画的方法 ,后面使用

 func beginRefresh(){

}

这时候下拉结束的方法就可以这样写了

  func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if !isRefreshing && progress >= 1{
//执行刷新任务
delegate?.doRefresh(self)
beginRefresh()
}
}

beginRefresh中,首先要把这个view固定在视野中,然后去执行动画 。 执行动画的时候我们希望下拉过程中不重新绘制了 我们定义一个变量var isAnimating = false

func beginRefresh(){
isRefreshing = true
isAnimating = true
UIView.animateWithDuration(0.3) {
var inSet = self.scrollView.contentInset;
inSet.top += self.frame.size.height
self.scrollView.contentInset = inSet
}
//动画
let keyAnimation = CAKeyframeAnimation(keyPath: "path")
keyAnimation.duration = 0.8
keyAnimation.values = [UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 20 , self.frame.size.height/2 - 15 , 10 , 10 )).CGPath ,
UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 8, self.frame.size.height/2 - 2 , 30 , 30 )).CGPath,
UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 18 , self.frame.size.height/2 - 18 , 20 , 20 )).CGPath,
UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 12 , self.frame.size.height/2 - 7 , 35 , 35 )).CGPath,
UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 17 , self.frame.size.height/2 - 17 , 28 , 28 )).CGPath,
UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 15 , self.frame.size.height/2 - 13 , 33 , 33 )).CGPath,
UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 15 , self.frame.size.height/2 - 15 , 30 , 30 )).CGPath
] self.shapeLayer.addAnimation(keyAnimation, forKey: nil) self.shapeLayer.path = UIBezierPath(ovalInRect: CGRectMake((self.frame.size.width/2) - 15 , self.frame.size.height/2 - 15 , 30 , 30 )).CGPath shapeLayer.fillColor = UIColor.clearColor().CGColor
shapeLayer.lineWidth = 4
shapeLayer.lineDashPattern = [2]
let baseAnimation = CABasicAnimation(keyPath: "strokeEnd")
baseAnimation.duration = 2
baseAnimation.fromValue = 0
baseAnimation.toValue = 1
baseAnimation.repeatDuration = 5
shapeLayer.addAnimation(baseAnimation, forKey: nil)
}

这个方法中前面 是固定住这个view然后,收松开后的动画了。。不多说了,大家任意发挥。可以搞自己喜欢的动画 。这里要有一个一直在走得效果我们设置strokeEnd从0-1 就是那个转得效果 。

shapeLayer.lineWidth = 4
shapeLayer.lineDashPattern = [2]

设置边框宽度 , 设置间隔,也可以设置多个如[2,4,3]这种 自己试试看效果

所以我们重新绘制的时候就需要判断是否正在动画 , 而且不需要这种间隔的效果了

//绘制
func reDraw(offY:CGFloat){
if !isAnimating {
shapeLayer.lineWidth = 0
shapeLayer.lineDashPattern = []
shapeLayer.fillColor = UIColor.grayColor().CGColor
let y = frame.size.height - offY + 1
let width = frame.size.width * progress * 0.8 > 15 ? 15:frame.size.width * progress * 0.8
shapeLayer.path = UIBezierPath(ovalInRect: CGRectMake((frame.size.width/2) - 7.5 , y , width , frame.size.height * progress * 0.8)).CGPath }
}

下载完成后需要结束动画 。 所以我们加了endRefresh方法

   func endRefresh(){
isRefreshing = false
isAnimating = false
UIView.animateWithDuration(0.3) {
var inSet = self.scrollView.contentInset;
inSet.top -= self.frame.size.height
self.scrollView.contentInset = inSet
}
}

代码并不多,这时候就可以使用了 。

首先 我们的viewController实现这个协议RefreshDelegate 。
然后在viewDidLoad中初始化

 let refreshView = DZRefreshViewTwo(scrollView: tableView)
refreshView.delegate = self
self.tableView.addSubview(refreshView)

然后实现协议方法就行了,

func doRefresh(refreshView: DZRefreshViewTwo) {
//在这里执行更新数据的操作 更新完执行endRefresh
}

我们这里模拟下,暂停4秒

  func doRefresh(refreshView: DZRefreshViewTwo) {
delay(4) {
refreshView.endRefresh()
}
} func delay(seconds: Double, completion:()->()) {
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64( Double(NSEC_PER_SEC) * seconds )) dispatch_after(popTime, dispatch_get_main_queue()) {
completion()
}
}

这时候执行效果如图

效果图
两个效果完整源码:https://github.com/smalldu/IOS-Animations
AnimationDemo7

Swift 自定义炫酷下拉刷新效果的更多相关文章

  1. [Swift通天遁地]二、表格表单-(4)使用系统自带的下拉刷新控件,制作表格的下拉刷新效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. [Swift通天遁地]二、表格表单-(6)创建美观的表格弹性下拉刷新效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. react-native-page-listview使用方法(自定义FlatList/ListView下拉刷新,上拉加载更多,方便的实现分页)

    react-native-page-listview 对ListView/FlatList的封装,可以很方便的分页加载网络数据,还支持自定义下拉刷新View和上拉加载更多的View.兼容高版本Flat ...

  4. 移动端上拉加载,下拉刷新效果Demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. css3 炫酷下拉菜单

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Android下拉刷新效果实现

    本文主要包括以下内容 自定义实现pulltorefreshView 使用google官方SwipeRefreshLayout 下拉刷新大致原理 判断当前是否在最上面而且是向下滑的,如果是的话,则加载数 ...

  7. android仿微信红包动画、Kotlin综合应用、Xposed模块、炫酷下拉视觉、UC浏览器滑动动画等源码

    Android精选源码 仿微信打开红包旋转动画 使用Kotlin编写的Android应用,内容你想象不到 Android手机上的免Root Android系统日志Viewer 一个能让微信 Mater ...

  8. 自定义ListView实现下拉刷新,下拉加载的功能

    package com.loaderman.myrefreshlistviewdemo; import android.content.Context; import android.util.Att ...

  9. 基于PtrFrameLayout实现自定义仿京东下拉刷新控件

    前言 最近基于项目需要,使用PtrFrameLayout框架实现了自定义的下拉刷新控件,大体效果类似于京东APP的下拉刷新动态效果.在这里和大家分享一下具体的思路和需要注意的地方,以便帮助有类似开发和 ...

随机推荐

  1. iOS 切割图片

    - (UIImage *)CutImageWithImage:(UIImage *)image withRect:(CGRect)rect { //使用CGImageCreateWithImageIn ...

  2. (转)CreateThread与_beginthreadex本质区别

    本文将带领你与多线程作第一次亲密接触,并深入分析CreateThread与_beginthreadex的本质区别,相信阅读本文后你能轻松的使用多线程并能流畅准确的回答CreateThread与_beg ...

  3. MysqlHelp

    using System.Configuration;using MySql.Data: public class MySqlHelp { //链接字符串 private static string ...

  4. Linux C 简易聊天室

    Linux下实现聊天室 介绍:程序在CentOS下,采用C语言实现,结构为Client/Server结构; 服务端程序通过共享存储区存储聊天数据,并发送给每个连接的客户端: 服务端程序和客户端程序都是 ...

  5. 怪兽z主机 驱动集

    这里给买家朋友送上我们主机的驱动包. 1.主板驱动.  访问密码 f334 http://yunpan.cn/QNTGxehcnLBW5 2.AMD显卡催化剂 amd catalyst(没装的话,无法 ...

  6. Dream

    即使下着雨,天空依旧明亮,因为远方有我的梦想.   ——forever97

  7. VS2010/MFC说明

    此栏目大多数内容转自鸡啄米http://www.jizhuomi.com/ 方便使用对其内容做了少量修改,仅是个人收藏使用,不做其它用途.

  8. hpu校赛--雪人的高度(离散化线段树)

    1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec  内存限制: 128 MB 提交: 81  解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...

  9. Swift:使用系统AVFoundation实现二维码扫描和生成

    系统提供的AVCaptureSession仅仅适用于iOS7.0以上的系统.之前的请用Zbar来替代 下载地址:http://download.csdn.net/detail/huobanbengku ...

  10. Python+Django+SAE系列教程9-----Django的视图和URL

    第三.四.五章介绍的就是Django中MVC的视图.模板.模型了. 首先来看视图(view),在用Django生成的站点目录中,创建一个view.py文件,这个文件開始是空的.然后我们输入下面内容: ...