UIRefreshControl是iOS10 的新特性之一,支持UIScrollView及其所有子类。
首先需要一个UIScrollView或者UIScrollView的子类,然后为其添加UIRefreshControl
具体做法如下:
import UIKitclass ViewController: UIViewController { var scrollvifew:UIScrollView! override func viewDidLoad() { super.viewDidLoad() //初始化scrollView let scrollview = UIScrollView(frame:UIScreen.main.bounds) //添加子控件 let view = UIView(frame:CGRect(x:0,y:0,width:scrollview.frame.size.width,height:800)) view.backgroundColor = UIColor.green scrollview.contentSize = view.frame.size scrollview.addSubview(view) self.view.addSubview(scrollview) //记录scrollview self.scrollvifew = scrollview //添加下拉刷新控件,支持UIScrollView及其所有子类 let refreshControl = UIRefreshControl() refreshControl.addTarget(self, action: #selector(refresh), for: UIControlEvents.valueChanged) scrollview.refreshControl = refreshControl } func refresh() { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2) { self.scrollvifew.refreshControl?.endRefreshing() } }}效果图:
新闻热点
疑难解答