         scrollView?.removeObserver(self, forKeyPath: "contentOffset", context: &KVOContext)
         scrollView?.removeObserver(self, forKeyPath: "contentInset", context: &KVOContext)
     }
-
+    
     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
         if context == &KVOContext {
             if keyPath == "contentOffset" {


class PullToRefresher: NSObject {
    func containerFrame(scrollView: UIScrollView) -> CGRect{
            let horizontalFrame = CGRect(x: -defaultDistanceToTrigger + animatorOffset.horizontal, y: animatorOffset.vertical, width: defaultDistanceToTrigger, height: scrollView.frame.height)
            let verticalFrame = CGRect(x: 0 + animatorOffset.horizontal, y: -defaultDistanceToTrigger + animatorOffset.vertical, width: scrollView.frame.width, height: defaultDistanceToTrigger)
            return direction == .horizontal ? horizontalFrame : verticalFrame
    }

    weak var scrollView: UIScrollView? {
        willSet {
            removeScrollViewObserving(scrollView)

    var updatingState = false
    var state: PullToRefreshState = .none {
        didSet {
            DispatchQueue.main.async {
                self.animator.animateState(self.state)
                switch self.state {
                case .none:
                    guard scrollView?.contentInset != self.defaultContentInset else { return }
                    if !self.scrollbackImmediately {
                        self.updatingState = true
                        self.scrollView?.setContentInset(self.defaultContentInset, completion: { [unowned self] (finished) -> Void in
                            self.updatingState = false
                        })
                    }
                    
                case .loading where oldValue != .loading:
                    if !self.scrollbackImmediately {
                        self.updatingState = true
                        var inset = self.defaultContentInset
                        if self.direction == .horizontal{
                            inset.left += self.defaultDistanceToTrigger
                        }
                        else{
                            inset.top += self.defaultDistanceToTrigger
                        }
                        self.scrollView?.setContentInset(inset, completion: { [unowned self] (finished) -> Void in
                            self.updatingState = false
                        })
                        self.action?()
                    }
                default:
                    break
                }
