[Swift]自作UIRefreshControl

自作UIRefreshControl

let REFRESH_HEIGHT:CGFloat = 66.0
var readyRefresh:Bool = false
var defaultInsetTop:CGFloat = 0.0

func scrollViewDidScroll(scrollView: UIScrollView) {
    if self.readyRefresh {
        if -self.REFRESH_HEIGHT < scrollView.contentInset.top + scrollView.contentOffset.y {
            self.readyRefresh = false
        }
    }else {
        if -self.REFRESH_HEIGHT >= scrollView.contentInset.top + scrollView.contentOffset.y {
            self.readyRefresh = true
        }
    }
}
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if !self.readyRefresh {
        return
    }
    self.readyRefresh = false
    self.defaultInsetTop = self.tableView.contentInsetTop
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.animateWithDuration(NSTimeInterval(0.3), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveLinear,
        animations: { () -> Void in
            self.tableView.contentInsetTop = self.REFRESH_HEIGHT
        },
        completion: { (finish:Bool) -> Void in
        }
    )
    
    self.executeRefresh()
}
private func endRefresh(){
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.animateWithDuration(NSTimeInterval(0.3), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveLinear,
        animations: { () -> Void in
            self.tableView.contentInsetTop = self.defaultInsetTop
        },
        completion: { (finish:Bool) -> Void in
        }
    )
}
private func executeRefresh(){
    //処理
    
    self.endRefresh()
}