Skip to content

Instantly share code, notes, and snippets.

View artturijalli's full-sized avatar

Artturi Jalli artturijalli

View GitHub Profile
@artturijalli
artturijalli / ViewController.swift
Created April 21, 2021 13:20
Add a smooth gradient to your app by pasting this view controller's contents to the view's controller you want to animate
import UIKit
class ViewController: UIViewController, CAAnimationDelegate {
let color1: CGColor = UIColor(red: 209/255, green: 107/255, blue: 165/255, alpha: 1).cgColor
let color2: CGColor = UIColor(red: 134/255, green: 168/255, blue: 231/255, alpha: 1).cgColor
let color3: CGColor = UIColor(red: 95/255, green: 251/255, blue: 241/255, alpha: 1).cgColor
let gradient: CAGradientLayer = CAGradientLayer()
var gradientColorSet: [[CGColor]] = []
@artturijalli
artturijalli / animateGradient.swift
Created April 21, 2021 14:18
A part of the functionality to animating gradients in Swift.
func animateGradient() {
/*
Assign the colors to the gradient layer that you want the animation to begin with.
These colors are given by the colorIndex:
*/
gradient.colors = gradientColorSet[colorIndex]
//Initialize a gradient animation object and assign an animation duration.
let gradientAnimation = CABasicAnimation(keyPath: "colors")
gradientAnimation.duration = 3.0
let color1: CGColor = UIColor(red: 209/255, green: 107/255, blue: 165/255, alpha: 1).cgColor
let color2: CGColor = UIColor(red: 134/255, green: 168/255, blue: 231/255, alpha: 1).cgColor
let color3: CGColor = UIColor(red: 95/255, green: 251/255, blue: 241/255, alpha: 1).cgColor
func setupGradient(){
// Create some color pairs for the gradient. The animator will juggle with these colors to create the gradients.
gradientColorSet = [
[color1, color2],
[color2, color3],
[color3, color1]
]
// Assign a color pair from the gradientColorSet array to the gradient layer. (The colorIndex is 0 ATM.)
@artturijalli
artturijalli / QRCode.swift
Created April 26, 2021 13:37
A simple swift function for generating QR codes
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
if let QRFilter = CIFilter(name: "CIQRCodeGenerator") {
QRFilter.setValue(data, forKey: "inputMessage")
guard let QRImage = QRFilter.outputImage else { return nil }
return UIImage(ciImage: QRImage)
}
return nil
@artturijalli
artturijalli / QRCodeScaled.swift
Created April 26, 2021 13:42
Generate a QR code in Swift and scale it up by 10
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
if let QRFilter = CIFilter(name: "CIQRCodeGenerator") {
QRFilter.setValue(data, forKey: "inputMessage")
guard let QRImage = QRFilter.outputImage else { return nil }
let scaleUp = CGAffineTransform(scaleX: 10.0, y: 10.0)
let scaledQR = QRImage.transformed(by: scaleUp)
@artturijalli
artturijalli / video_to_gif_convert.py
Created April 27, 2021 06:34
Convert video to gif with 3 lines of code. Install Tkinter with brew install python-tk. Install moviepy with pip install moviepy
from moviepy.editor import VideoFileClip
from tkinter.filedialog import *
video_file = askopenfilename()
clip = VideoFileClip(video_file)
clip.write_gif("ouput.gif", fps=10)
@artturijalli
artturijalli / video_to_gif_convert_improved.py
Created April 27, 2021 07:43
Convert video to gif with 3 lines of code. Install Tkinter with brew install python-tk. Install moviepy with pip install moviepy
from moviepy.editor import VideoFileClip
from tkinter.filedialog import *
import os
# Accept only .mov and .mp4 files. Feel free to change.
accepted_files = [("Mov files", "*.mov"), ("MP4 files", "*.mp4")]
# Select a video which is one of the accepted file types from your machine
video_file = askopenfilename(filetypes=accepted_files)
@artturijalli
artturijalli / generics.swift
Created April 28, 2021 14:40
An example of generics in Swift. Using the generic type for Vec3D you can create vectors of ints, floats, doubles with a single implementation of the structure.
struct Vec3D<T> {
let x, y, z: T
init(x: T, y: T, z: T) {
self.x = x
self.y = y
self.z = z
}
}
let intVector = Vec3D(x: 1, y: 2, z: 5)
@artturijalli
artturijalli / inoutExample.swift
Created April 28, 2021 14:41
Inouts can be used to change a function's parameter inside the function.
func change(_ number: inout Int){
number = 2
}
var number = 1
change(&number)
print(number)
// Output: