iOS10.0原生刷新控件UIRefreshControl的简单使用

@available(iOS 10.0, *)
open var refreshControl: UIRefreshControl?
class ViewController: UIViewController {
    
    var scrollView: UIScrollView?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 创建UIScrollView
        let scrollView = UIScrollView(frame: UIScreen.main.bounds)
        
        // 初始化UIScrollView
        let v = UIView(frame: CGRect(x: 0, y: 0, width: scrollView.frame.width, height: 700))
        v.backgroundColor = UIColor.purple
        scrollView.addSubview(v)
        scrollView.contentSize = v.frame.size
        
        self.view.addSubview(scrollView)
        self.scrollView = scrollView
        
        // 创建UIRefreshControl
        let refreshControl = UIRefreshControl()
        scrollView.refreshControl = refreshControl
        refreshControl.addTarget(self, action: #selector(refresh), for: UIControlEvents.valueChanged)
        
        // 将UIRefreshControl添加到UIScrollView上
        scrollView.refreshControl = refreshControl
        
    }
    
    func refresh() {
        print("刷新事件执行了")
        
        // 2秒后结束刷新
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            self.scrollView?.refreshControl?.endRefreshing()
        }
    }
    
}
    原文作者:watayouxiang
    原文地址: https://segmentfault.com/a/1190000007577088
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞