当前位置: > > > Swift - MJRefresh库的使用详解7(ScrollView上实现上拉下拉刷新)

Swift - MJRefresh库的使用详解7(ScrollView上实现上拉下拉刷新)

MJRefresh 支持如下控件的刷新:UITableViewUICollectionViewUIWebViewUIScrollView。前面三个我在之前的文章已经介绍过了,本文演示 UIScrollView 上如何实现下拉刷新、上拉加载更多数据。

一、下拉刷新
1,样例效果
(1)初始化的时候生成20个随机颜色的色块。
(2)下拉 scrollView 即可重新生成数据并刷新。

2,样例代码
import UIKit

class ViewController: UIViewController {
    
    var scrollView:UIScrollView!
    
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //scrollView初始化
        scrollView = UIScrollView()
        scrollView.frame = self.view.bounds
        self.view.addSubview(scrollView)
        
        //初始化数据
        refreshItemData()
        
        //下拉刷新相关设置
        header.setRefreshingTarget(self, refreshingAction: #selector(ViewController.headerRefresh))
        scrollView.mj_header = header
    }
    
    //初始化数据
    func refreshItemData() {
        //先清空内部元素
        for v in self.scrollView.subviews{
            //如果不是下拉组件,则移除
            if v != header {
                v.removeFromSuperview()
            }
        }
        
        // 增加20条假数据
        for i in 0...19{
            let itemView = UIView(frame: CGRect(x: 0, y: i*60,
                                                width: Int(self.view.bounds.width), height: 60))
            itemView.backgroundColor = UIColor.randomColor
            scrollView.addSubview(itemView)
        }
        
        //设置内容区域尺寸
        scrollView.contentSize = CGSize(width: Int(self.view.bounds.width), height: 20 * 60)
    }
    
    //顶部下拉刷新
    func headerRefresh(){
        print("下拉刷新.")
        sleep(2)
        //重现生成数据
        refreshItemData()
        //结束刷新
        scrollView.mj_header.endRefreshing()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}

extension UIColor {
    //返回随机颜色
    open class var randomColor:UIColor{
        get
        {
            let red = CGFloat(arc4random()%256)/255.0
            let green = CGFloat(arc4random()%256)/255.0
            let blue = CGFloat(arc4random()%256)/255.0
            return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
        }
    }
}
源码下载hangge_1415.zip


二、上拉加载
1,样例效果
(1)初始化的时候生成10个随机颜色的方块。
(2)上拉 scrollView 即可继续新增10个方块并加载进来。

2,样例代码
import UIKit

class ViewController: UIViewController {
    
    var scrollView:UIScrollView!
    
    //条目数
    var itemCount = 0
    
    // 底部加载
    let footer = MJRefreshAutoNormalFooter()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //scrollView初始化
        scrollView = UIScrollView()
        scrollView.frame = self.view.bounds
        self.view.addSubview(scrollView)
        
        //初始化数据
        refreshItemData()
        
        //上拉加载相关设置
        footer.setRefreshingTarget(self, refreshingAction: #selector(ViewController.footerLoad))
        scrollView.mj_footer = footer
    }
    
    //初始化数据
    func refreshItemData() {
        // 增加10条假数据
        for _ in 1...10{
            let itemView = UIView(frame: CGRect(x: 0, y: itemCount*60,
                                                width: Int(self.view.bounds.width), height: 60))
            itemView.backgroundColor = UIColor.randomColor
            scrollView.addSubview(itemView)
            itemCount += 1
        }
        //设置内容区域尺寸
        scrollView.contentSize = CGSize(width: Int(self.view.bounds.width), height: itemCount * 60)
    }
    
    //底部上拉加载
    func footerLoad(){
        print("上拉加载.")
        sleep(2)
        //生成并添加数据
        refreshItemData()
        //结束刷新
        scrollView.mj_footer.endRefreshing()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension UIColor {
    //返回随机颜色
    open class var randomColor:UIColor{
        get
        {
            let red = CGFloat(arc4random()%256)/255.0
            let green = CGFloat(arc4random()%256)/255.0
            let blue = CGFloat(arc4random()%256)/255.0
            return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
        }
    }
}
源码下载hangge_1415.zip
评论0