0

Here is the code with Swift3.0

private let MAX_HEIGHT: CGFloat = 100.0

override func viewDidLoad() {
    super.viewDidLoad()

    self.spinner = UIActivityIndicatorView(frame: CGRect(x: 0, y: -MAX_HEIGHT, width: self.view.bounds.width, height: MAX_HEIGHT))
    self.spinner.activityIndicatorViewStyle = .whiteLarge
    self.spinner.color = .blue
    self.view.insertSubview(spinner, at: 0)
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 20
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)
    cell.textLabel?.text = "The \(indexPath.row) Row"

    return cell
}

And Here is the important code: When I drag the list, it will do this

override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if -scrollView.contentOffset.y >= MAX_HEIGHT + scrollView.contentInset.top {
        UIView.animate(withDuration: 0.5, animations: {
            scrollView.contentInset.top += MAX_HEIGHT
        })
        self.update()
    }
}

func update() {
    self.spinner.startAnimating()
    // I want to make a sleep for 2 seconds, But sleep() is not suit 
    UIView.animate(withDuration: 2.0, animations: {
        self.view.alpha = 0.99 
    }, completion: { (_) -> Void in
        self.view.alpha = 1.0
        self.spinner.stopAnimating()
        UIView.animate(withDuration: 0.5, animations: {
            self.tableView.contentInset.top -= MAX_HEIGHT
        })
    })
}

The Main View

The animation I want is when I finish my drag, the list will have a update animation like this

Updating

The scrollview.contentInset.top will be set as 100.0 when I finish my dragging. If I dragged a bit longer(125.0 ..), the list will have an animation like sliding slowly from the position I dragged to 100.0

But the result is that when I finished my dragging, the list will slide a bit longer and then slide to 100.0, it didn't slide to 100.0 directly. It looks like the list make a quick shake.

halfer
  • 19,824
  • 17
  • 99
  • 186
pluto
  • 516
  • 6
  • 9
  • Do you know that this functionality is already handled when using [UIRefreshControl](https://developer.apple.com/reference/uikit/uirefreshcontrol)? you might want to check [this question](http://stackoverflow.com/questions/24475792/how-to-use-pull-to-refresh-in-swift-2) to know how to use it. – Ahmad F Dec 18 '16 at 08:31
  • using UIRefreshControl seems a promising solution, but if I want to add some custom animation, the UIRefreshControl cannot help a lot. I have to write a custom control like that – pluto Dec 18 '16 at 14:11
  • So, now the idea is: you should try to find out how you can create your custom UIRefreshControl (custom animation). – Ahmad F Dec 18 '16 at 15:29
  • Ok,I will try to find some documents. – pluto Dec 19 '16 at 03:30

1 Answers1

1

Try using UIRefreshControl

Maybe this tutorial will help you https://www.appcoda.com/pull-to-refresh-uitableview-empty/

  • Okay,this help me a lot. I haven't use this control before, which seems it's a promising solution..Although the UIRefreshControl can make a system update list, I cannot use it to make a custom update list, such as a update list with a dynamic effect when it refreshing.So, I also want to solve my old question. Thanks anyway! – pluto Dec 18 '16 at 13:59