修改 pull 以快速刷新 [英] modify pull to refresh in swift

查看:25
本文介绍了修改 pull 以快速刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户拉动 tableView 刷新时,我试图添加一个图标(向下).图标向用户显示(向下指示器).这是我的代码

I'm trying to add an icon (downward) when the user pulls the tableView to refresh. the icon shows(downward indicator) to the user. here my code is

    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 上有一个向下的箭头图标

but I want a downward arrow icon on collectionView before the refresh indicator

推荐答案

这是为 UIRefreshControl 创建自定义类的模板:

Here is a template for creating your custom class for 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)
     }
}

这篇关于修改 pull 以快速刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆