I am building a native iOS wrapper for a web site that looks and behaves like a magazine. It will have long scrolling pages but the user can also swipe between chapters. So, what I would like to do is load a new HTML page but have it transition in from the left or right as if it's been pushed/popped off a navigation stack. Does anyone know if it is possible to inject some core animation into the WKWebView component to achieve this effect?
Asked
Active
Viewed 1,496 times
1 Answers
2
One way you can implement this is making an animation between two WKWebViews. You load your first WKWebView, then, when you activate the transition, you create a second WKWebView, place it offscreen and animate it's entrance at the same time you move the current webView offscreen and then deatach it from your view. Something like:
func presentNextWebView() {
let webViewToRemove = currentWebView
let webViewToAdd = WKWebView()
webViewToAdd.frame = webViewToRemove.frame
webViewToAdd.center = CGPointMake(2.0*self.view.bounds.width, CGRectGetMidY(self.view.bounds))
currentWebView = webViewToAdd
self.view.addSubview(webViewToAdd)
UIView.animateWithDuration(1.0, animations: {
webViewToRemove.center = CGPointMake(-2.0*self.view.bounds.width, CGRectGetMidY(self.view.bounds))
webViewToAdd.center = self.CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds))
}, completion: { finished in
webViewToRemove.removeFromSuperview()
})
}

jackbravo
- 1,329
- 1
- 12
- 17
-
The problem _was_ that all of the navigation between pages was done in the HTML using JavaScript. So as well as vertical scrolling you were able to swipe left and right to go to previous and next pages. I wanted to somehow change the way the WKWebView loads a new page in itself. – Lee Probert Sep 24 '16 at 06:24