首页 > 系统 > iOS > 正文

iOS10新特性之UIRefreshControl的使用

2019-11-06 09:57:29
字体:
来源:转载
供稿:网友

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() } }}

效果图: 这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表