This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if UIScreen.main.bounds.height < 812 { | |
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 100, initialSpringVelocity: 30, options: [.curveEaseOut, .allowUserInteraction], animations: { | |
self.frame.origin = nearestCornerPosition | |
}, completion: nil) | |
} else { | |
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 100, initialSpringVelocity: 10, options: [.curveEaseOut, .allowUserInteraction], animations: { | |
self.frame.origin = nearestCornerPosition | |
}, completion: nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func project(initialVelocity: CGFloat, decelerationRate: CGFloat) -> CGFloat { | |
return (initialVelocity / 1000) * decelerationRate / (1 - decelerationRate) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@objc func wasDragged(gesture: UIPanGestureRecognizer) { | |
if gesture.state == .began || gesture.state == .changed { | |
let translation = gesture.translation(in: superview!) | |
guard let currentPosition = currentSnapPosition else { return } | |
frame.origin = CGPoint(x: currentPosition.x + translation.x, y: currentPosition.y + translation.y) | |
} | |
if gesture.state == .ended { | |
let velocity = gesture.velocity(in: superview!) | |
let decelerationRate = UIScrollView().decelerationRate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setupGestureRecognizer() { | |
let gesture = UIPanGestureRecognizer(target: self, action: #selector(wasDragged(gesture:))) | |
addGestureRecognizer(gesture) | |
isUserInteractionEnabled = true | |
} | |
@objc func wasDragged(gesture: UIPanGestureRecognizer) { | |
if gesture.state == .began || gesture.state == .changed { | |
let translation = gesture.translation(in: superview!) | |
guard let currentPosition = currentSnapPosition else { return } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addPerson(name, age) { | |
//Zo simpel is het: deze functie roepen we aan vanuit de native code. | |
console.log('Name: ' + name + ' Age: ' + age); | |
} | |
function sendNameToNative(name) { | |
//Deze functie zal een naam naar de native code sturen. | |
//We kunnen een EventListener aan een object toevoegen om deze functie aan te roepen als iemand er op klikt. | |
try { | |
webkit.messageHandlers.callback.postMessage(name); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ViewController: WKScriptMessageHandler, WKNavigationDelegate { | |
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { | |
//Deze functie verwerkt de berichten vanuit javascript. | |
//We kunnen hier de meegestuurde data vinden in de message body: | |
guard let response = message.body as? String else { return } | |
print(response) | |
} | |
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sendToJavascript(name: String, age: Int) { | |
webview.evaluateJavaScript("addPerson('\(name)', \(age))", completionHandler: nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func loadView() { | |
super.loadView() | |
let contentController = WKUserContentController() | |
contentController.add(self, name: "callback") | |
let config = WKWebViewConfiguration() | |
config.userContentController = contentController | |
webview = WKWebView(frame: webview.frame, configuration: config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addPerson(name, age) { | |
//It's really this simple. We'll call this from the native code. | |
console.log('Name: ' + name + ' Age: ' + age); | |
} | |
function sendNameToNative(name) { | |
//This function will send the name to native. | |
//We can add an EventListener to an object on the page to call this function when it is clicked. | |
try { | |
webkit.messageHandlers.callback.postMessage(name); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ViewController: WKScriptMessageHandler, WKNavigationDelegate { | |
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { | |
//This function handles the events coming from javascript. We'll configure the javascript side of this later. | |
//We can access properties through the message body, like this: | |
guard let response = message.body as? String else { return } | |
print(response) | |
} | |
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { |
NewerOlder