ThornTechPublic / InteractiveModal

MIT License
148 stars 36 forks source link

Trying this out with UIWebKit Anyideas? #7

Open alexrmacleod opened 7 years ago

alexrmacleod commented 7 years ago

So I wonder how I could get this to work with WKNavigationDelegate, WKUIDelegate? below is my first attempt inside the BrowserViewController which isn't working.

`

let pan = UIPanGestureRecognizer(target: self, action: #selector(handleGesture(sender:)))
    pan.delegate = self
    webView.scrollView.addGestureRecognizer(pan)
}

func progressAlongAxis(pointOnAxis: CGFloat, axisLength: CGFloat) -> CGFloat {
    let movementOnAxis = pointOnAxis / axisLength
    let positiveMovementOnAxis = fmaxf(Float(movementOnAxis), 0.0)
    let positiveMovementOnAxisPercent = fminf(positiveMovementOnAxis, 1.0)
    return CGFloat(positiveMovementOnAxisPercent)
}

@IBAction func handleGesture(sender: UIPanGestureRecognizer) {

    let percentThreshold:CGFloat = 0.3

    // convert y-position to downward pull progress (percentage)
    let translation = sender.translation(in: view)
    // using the helper method
    let progress = progressAlongAxis(pointOnAxis: translation.y, axisLength: view.bounds.height)

    guard let interactor = interactor,
        let originView = sender.view else { return }

    print("#\(webView.scrollView.contentOffset.y)")
    // Only let the table view dismiss the modal only if we're at the top.
    // If the user is in the middle of the table, let him scroll.
    switch originView {
    case view:
        break
    case webView:
        if webView.scrollView.contentOffset.y > 0 {
            return
        }
    default:
        break
    }

    switch sender.state {
    case .began:
        interactor.hasStarted = true
        dismiss(animated: true, completion: nil)
    case .changed:
        interactor.shouldFinish = progress > percentThreshold
        interactor.update(progress)
    case .cancelled:
        interactor.hasStarted = false
        interactor.cancel()
    case .ended:
        interactor.hasStarted = false
        interactor.shouldFinish
            ? interactor.finish()
            : interactor.cancel()
    default:
        break
    }
}

`

Do I have to use PanGesture? Thinking about putting this logic into the webview contentoffset listner. here:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {