изменить тягу, чтобы быстро обновить
Я пытаюсь добавить значок (вниз), когда пользователь тянет tableView для обновления. значок показывает (индикатор вниз) пользователю. вот мой код
var refreshControl = UIRefreshControl()
let arr = ["first row ","2nd row ","3rd row ","4th row ","5th row ","6th row ","7th row ","8th row ","9th row"]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
tableView.addSubview(refreshControl)
tableView.rowHeight = 160
}
@objc func refresh(_ sender: AnyObject) {
refreshControl.endRefreshing()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = arr[indexPath.row]
return cell
}
но мне нужен значок стрелки вниз в collectionView перед индикатором обновления
1 ответ
Вот шаблон для создания собственного класса для UIRefreshControl:
class CustomRefreshControl: UIRefreshControl {
fileprivate var isAnimating = false
fileprivate let maxPullDistance: CGFloat = 150
override init() {
super.init(frame: .zero)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateProgress(with offsetY: CGFloat) {
guard !isAnimating else { return }
// progress
let _ = min(abs(offsetY / maxPullDistance), 1)
}
override func beginRefreshing() {
super.beginRefreshing()
isAnimating = true
}
override func endRefreshing() {
super.endRefreshing()
isAnimating = false
}
func setupView() {
tintColor = .clear
addTarget(self, action: #selector(beginRefreshing), for: .valueChanged)
}
}