首先,声明一个UIRefreshControl变量属性
/// 刷新控件
var refreshControl: UIRefreshControl?
设置刷新控件
//设置刷新控件
// 1>实例化控件
refreshControl = UIRefreshControl();
// 2>添加到视图
tableView?.addSubview(refreshControl!);
// 3>添加监听方法
refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged);
UIRefreshControl头文件
import Foundation
import UIKit
//
// UIRefreshControl.h
// UIKit
//
// Copyright 2012-2015 Apple Inc. All rights reserved.
//
@available(iOS 6.0, *)
open class UIRefreshControl : UIControl {
/* The designated initializer
* This initializes a UIRefreshControl with a default height and width.
* Once assigned to a UITableViewController, the frame of the control is managed automatically.
* When a user has pulled-to-refresh, the UIRefreshControl fires its UIControlEventValueChanged event.
*
*/
public init()
open var isRefreshing: Bool { get }
open var tintColor: UIColor!
open var attributedTitle: NSAttributedString?
// May be used to indicate to the refreshControl that an external event has initiated the refresh action
@available(iOS 6.0, *)
open func beginRefreshing()
// Must be explicitly called when the refreshing has completed
@available(iOS 6.0, *)
open func endRefreshing()
}
When a user has pulled-to-refresh, the UIRefreshControl fires its UIControlEventValueChanged event.
当执行拉动刷新操作时,UIRefreshControl会相应UIControlEventValueChanged事件。
所以 addTarget 方法 UIControlEvents 枚举中选择 .valueChanged