Skip to content

Instantly share code, notes, and snippets.

View JillevdW's full-sized avatar

Jille van der Weerd JillevdW

View GitHub Profile
@JillevdW
JillevdW / check.swift
Last active July 6, 2018 23:48
phone check
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)
}
private func project(initialVelocity: CGFloat, decelerationRate: CGFloat) -> CGFloat {
return (initialVelocity / 1000) * decelerationRate / (1 - decelerationRate)
}
@JillevdW
JillevdW / dragging.swift
Last active July 6, 2018 23:48
No longer naive
@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
@JillevdW
JillevdW / naivedragging.swift
Last active July 6, 2018 09:31
Naive dragging
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 }
@JillevdW
JillevdW / dutch.js
Created February 23, 2018 10:44
javascriptnederlands
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);
@JillevdW
JillevdW / vcdutch.swift
Created February 23, 2018 10:41
delegate functions Nederlands
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!) {
@JillevdW
JillevdW / send.swift
Last active May 26, 2019 17:53
send message to javascript
func sendToJavascript(name: String, age: Int) {
webview.evaluateJavaScript("addPerson('\(name)', \(age))", completionHandler: nil)
}
@JillevdW
JillevdW / vc.swift
Last active February 23, 2018 09:58
final setup medium webview
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)
@JillevdW
JillevdW / webview.js
Last active February 23, 2018 11:05
webview medium javascript
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);
@JillevdW
JillevdW / vc.swift
Created February 23, 2018 09:23
ViewController conforms WKSMH and WKND
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!) {