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 animation I want is when I finish my drag, the list will have a update animation like this
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.