材料"拉动刷新"在斯威夫特?

时间:2016-03-02 15:46:58

标签: ios swift pull-to-refresh

我正在尝试在WKWebView中实现pull刷新,就像在Material pull to refresh GIF中一样。因为我们想要包含已经有html导航栏的网站,所以我们需要在向下拖动时保持Web视图的修复。我发现刷新(UIRefreshControl)到表视图和Web视图,但视图随着用户拖动而下降。

我将委托设置为Web视图的scrollView属性并接收通知。当我拖到顶部并且到达0(垂直滚动视图偏移)时,我可以禁用滚动视图,启用平移手势。但要实际移动自定义微调器视图,我需要再次触摸。

方法func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)不起作用,因为我有一个滚动视图。

private func gestures() {

    self.panGesture = UIPanGestureRecognizer(
        target: self,
        action: "panGestureCaptured:"
    )
    self.panGesture!.enabled = false
    self.webView.addGestureRecognizer(self.panGesture!)
}

func scrollViewDidScroll(scrollView: UIScrollView) {

    if scrollView.contentOffset.y <= 0 {
    self.view.layoutIfNeeded()
        self.spinnerTopLayoutConstraint?.constant = -scrollView.contentOffset.y
    }
}

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {

    if scrollView.panGestureRecognizer.translationInView(scrollView.superview).y > 0 { // dragging down

        if scrollView.contentOffset.y == 0 {

            self.webView!.scrollView.scrollEnabled = false
          //  self.webView!.scrollView.canCancelContentTouches = true
            self.panGesture!.enabled = true
            // self.refreshWebView()


        }

        self.showNavigationItems()


    } else { // dragging up
        self.hideNavigationItems()
    }
}

2 个答案:

答案 0 :(得分:1)

感谢@Darko:我们的想法是使用滚动视图的panGesture属性(也是Web视图的属性)。

private func gestures() {

    self.webView.scrollView.panGestureRecognizer.addTarget(
        self,
        action: "panGestureCaptured:"
    )
}

func panGestureCaptured(gesture: UIGestureRecognizer) {
    let touchPoint = gesture.locationInView(self.webView)

    print("touchPoint: \(touchPoint)")

    print("panGestureCaptured scrollView offset \(self.webView!.scrollView.contentOffset.y)")

    if self.webView!.scrollView.contentOffset.y == 0 {


        if self.webView!.scrollView.panGestureRecognizer.state == UIGestureRecognizerState.Changed {

            if touchPoint.y < self.webView!.frame.height * 0.3 {
                self.spinnerTopLayoutConstraint?.constant = touchPoint.y
            } else {
                self.spinnerTopLayoutConstraint?.constant = self.webView!.frame.height * 0.3
            }

        } else if self.webView!.scrollView.panGestureRecognizer.state == UIGestureRecognizerState.Ended {
            self.spinnerTopLayoutConstraint?.constant = UIApplication.sharedApplication().statusBarFrame.height + 20
        }
    }
}

答案 1 :(得分:1)

WebView的滚动视图已经有一个pan识别器(webView.scrollView.panGestureRecognizer),因此你可以听到这个(使用addTarget)并禁用滚动视图上的弹跳。这样您就不需要禁用/启用平移识别器。