ios – How can I slowly scroll up and down my desk view from a background thread?


Remark this line

Thread.sleep(forTimeInterval: 0.2)

inside DispatchQueue.important.async because it makes the principle thread sleep for some time , however it’s worthwhile to solely make the background thread to do the t sleep solely

Edit: with out utilizing scrollToRow you possibly can consider contentOffset


struct Nation {
    var isoCode: String
    var identify: String
}

class CountriesTableViewController: UITableViewController {
    var scrollingMode = true
    let nations = [
        Country(isoCode: "at", name: "Austria"),
        Country(isoCode: "be", name: "Belgium"),
        Country(isoCode: "de", name: "Germany"),
        Country(isoCode: "el", name: "Greece"),
        Country(isoCode: "fr", name: "France"),
        Country(isoCode: "at", name: "Austria"),
        Country(isoCode: "be", name: "Belgium"),
        Country(isoCode: "de", name: "Germany"),
        Country(isoCode: "el", name: "Greece"),
        Country(isoCode: "fr", name: "France"),
        Country(isoCode: "at", name: "Austria"),
        Country(isoCode: "be", name: "Belgium"),
        Country(isoCode: "de", name: "Germany"),
        Country(isoCode: "el", name: "Greece"),
        Country(isoCode: "fr", name: "France"),
        Country(isoCode: "at", name: "Austria"),
        Country(isoCode: "be", name: "Belgium"),
        Country(isoCode: "de", name: "Germany"),
        Country(isoCode: "el", name: "Greece"),
        Country(isoCode: "fr", name: "France"),
        Country(isoCode: "at", name: "Austria"),
        Country(isoCode: "be", name: "Belgium"),
        Country(isoCode: "de", name: "Germany"),
        Country(isoCode: "el", name: "Greece"),
        Country(isoCode: "fr", name: "France"),
        Country(isoCode: "at", name: "Austria"),
        Country(isoCode: "be", name: "Belgium"),
        Country(isoCode: "de", name: "Germany"),
        Country(isoCode: "el", name: "Greece"),
        Country(isoCode: "fr", name: "France"),
        Country(isoCode: "at", name: "Austria"),
        Country(isoCode: "be", name: "Belgium"),
        Country(isoCode: "de", name: "Germany"),
        Country(isoCode: "el", name: "Greece"),
        Country(isoCode: "fr", name: "France"),
        Country(isoCode: "at", name: "Austria"),
        Country(isoCode: "be", name: "Belgium"),
        Country(isoCode: "de", name: "Germany"),
        Country(isoCode: "el", name: "Greece"),
        Country(isoCode: "fr", name: "France"),
        Country(isoCode: "at", name: "Austria"),
        Country(isoCode: "be", name: "Belgium"),
        Country(isoCode: "de", name: "Germany"),
        Country(isoCode: "el", name: "Greece"),
        Country(isoCode: "fr", name: "France"),
    ]

    // MARK: - Desk view knowledge supply
    
    var top:CGFloat = 40
    
    override func viewDidAppear(_ animated: Bool) {
        tremendous.viewDidAppear(animated)
        DispatchQueue.important.asyncAfter(deadline: .now() + 2) {
            self.startBackgroundScrollingThread()
        }
    }
    
    func startBackgroundScrollingThread() {
        let max = self.tableView.contentSize.top - self.tableView.body.top
        DispatchQueue.world(qos: .background).async {
            whereas self.scrollingMode && self.top < max {
                DispatchQueue.important.async {
                    //Thread.sleep(forTimeInterval: 0.2)
                    self.tableView.contentOffset = CGPoint(x: self.tableView.contentOffset.x, y: self.top)
                    self.top += 5
                    print(self.top)
                }
                Thread.sleep(forTimeInterval: 0.2)
            }
            
            whereas self.scrollingMode && self.top >= 0 {
                DispatchQueue.important.async {
                    //Thread.sleep(forTimeInterval: 0.2)
                    self.tableView.contentOffset = CGPoint(x: self.tableView.contentOffset.x, y: self.top)
                    self.top -= 5
                    print(self.top)
                }
                Thread.sleep(forTimeInterval: 0.2)
            }
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        self.nations.rely
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CountryCell", for: indexPath)

        let nation = self.nations[indexPath.row]
        cell.textLabel?.textual content = nation.identify
        cell.detailTextLabel?.textual content = nation.isoCode
        cell.imageView?.picture = UIImage(named: nation.isoCode)

        return cell
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection part: Int) -> String? {
        "Nations"
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles