coceki
coceki

Reputation: 173

modify pull to refresh in swift

I'm trying to add an icon (downward) when the user pulls the tableView to refresh. the icon shows(downward indicator) to the user. in this screenshot I want to dismiss this arrow when the loader is started and shows an arrow when the user starts to pull the tableView 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
    }

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

Upvotes: 1

Views: 1262

Answers (1)

Alirza Eram
Alirza Eram

Reputation: 178

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

Upvotes: 3

Related Questions