好吧,过了一段时间,有了上面所有的 cmets 和答案,我想出了一些东西。
使用 scrollViewDidScroll 我实现了底部刷新
如果 UIScrollView 已到达底部,它会向上滑动 UIView,并在执行后自动向下滑动并重新加载 UITableView。
scrollViewDidScroll 函数
func scrollViewDidScroll(scrollView: UIScrollView) {
let scrollOffset : CGFloat = scrollView.contentOffset.y
let scrollHeight : CGFloat = scrollView.frame.size.height
let scrollContentSizeHeight : CGFloat = scrollView.contentSize.height + scrollView.contentInset.bottom
let tableFrame : CGRect = self.tableView.frame
if (scrollOffset + scrollHeight) >= scrollContentSizeHeight {
self.bottomRefreshAnimation()
} else {
if self.tableView.frame.origin.y < 0 {
UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.tableView.frame.origin.y = self.tableView.frame.origin.y + 40
}, completion: nil)
}
}
}
self.bottomRefreshAnimation() 包含从底部为 UIView 设置动画的代码
func bottomRefreshAnimation(){
if self.tableView.frame.origin.y > 0 {
UIView.animateWithDuration(0.4, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.tableView.frame.origin.y = self.tableView.frame.origin.y - 40
}, completion: nil)
populateData({ (finished) -> () in
UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
if self.tableView.frame.origin.y < 0 {
self.tableView.frame.origin.y = self.tableView.frame.origin.y + 40
}
}, completion: nil)
})
}
}
最重要的是它检查 UITableView Origin.y 并在 UIView 向上滑动时将大小减小 40。
希望对大家有所帮助
如果有更好的方法来实现这一点,请告诉我
感谢大家的回复。