Skip to content

Instantly share code, notes, and snippets.

@sagaya
sagaya / luhn_algorithm.swift
Created May 15, 2017 23:39
Luhn Algorithm for generating random valid credit card numbers with swift
var card_number = ""
func make_random_number(num:Int)->[Int]{
var result = [Int]()
for _ in 0...num {
result.append(Int(arc4random_uniform(9)))
}
return result
}
func luhn_algorithm(){
@sagaya
sagaya / luhn_algorithm.py
Created May 16, 2017 00:51
Luhn Algorithm for generating random valid credit card numbers with Python
import random
credit_card_number = ""
def make_random_number(number_of_element):
random_numbers = []
for i in range(number_of_element):
random_numbers.append(random.randint(0, 9))
return random_numbers
@sagaya
sagaya / bezier_path.swift
Created January 20, 2019 15:26
Curved Bezier Path
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 25.5, y: 185.5))
bezierPath.addCurve(to: CGPoint(x: 25.5, y: 56.5), controlPoint1: CGPoint(x: 25.5, y: 185.5), controlPoint2: CGPoint(x: 25.5, y: 88.75))
bezierPath.addCurve(to: CGPoint(x: 72.5, y: 67.5), controlPoint1: CGPoint(x: 25.5, y: 24.25), controlPoint2: CGPoint(x: 60.75, y: 64.75))
bezierPath.addCurve(to: CGPoint(x: 108.5, y: 185.5), controlPoint1: CGPoint(x: 84.25, y: 70.25), controlPoint2: CGPoint(x: 108.5, y: 185.5))
bezierPath.addCurve(to: CGPoint(x: 136.5, y: 299.5), controlPoint1: CGPoint(x: 108.5, y: 185.5), controlPoint2: CGPoint(x: 129.5, y: 271))
bezierPath.addCurve(to: CGPoint(x: 186.5, y: 279.5), controlPoint1: CGPoint(x: 143.5, y: 328), controlPoint2: CGPoint(x: 174, y: 284.5))
bezierPath.addCurve(to: CGPoint(x: 202.5, y: 185.5), controlPoint1: CGPoint(x: 199, y: 274.5), controlPoint2: CGPoint(x: 202.5, y: 185.5))
bezierPath.addCurve(to: CGPoint(x: 217.5, y: 67.5), controlPoint1: CGPoint(x: 202.5, y: 185.5), controlPoint2: CG
@sagaya
sagaya / animate_curve.swift
Last active January 20, 2019 15:37
iOS animate along curve
let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = bezierPath.cgPath
animation.repeatCount = 0
animation.duration = 3.0
animation.delegate = self
movingView.layer.add(animation, forKey: "animate along path")
movingView.center = CGPoint(x: 0, y: frame.maxY)
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
movingView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
@sagaya
sagaya / animate_scale.swift
Last active January 20, 2019 15:51
animate scale ios
fileprivate func scaleMethod() {
UIView.animate(withDuration: 0.75, animations: {
self.movingView.transform = .identity
}) { _ in
self.movingView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
self.scaleMethod()
}
}
@sagaya
sagaya / generic_controller.swift
Created January 21, 2019 17:04
generic_controller
class BaseController<ViewType: UIView>: UIViewController, Controller {
var viewType: ViewType{
if let view = self.view as? ViewType {
return view
} else {
let view = ViewType()
self.view = view
return view
}
}
@sagaya
sagaya / using_generic_controller.swift
Last active January 21, 2019 17:18
using_generic_controller
class TestViewController: BaseController<OriginalView> {
}
class TestBaseView: BaseView {
public weak var buttonActionDelegate: MyViewActionDelegate?
private let button: UIButton = {
@sagaya
sagaya / maincoordinator.swift
Created January 29, 2019 15:14
maincoordinator
class MainCoordinator: Coordinator {
var childCoodinators = [Coordinator]()
var navigationController: UINavigationController
fileprivate var isLoggedIn = false
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
func start() {
@sagaya
sagaya / maincoordinator2.swift
Created January 29, 2019 15:26
maincoordinator2
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UINavigationController()
mainCoordinator = MainCoordinator(navigationController: window?.rootViewController as! UINavigationController)
mainCoordinator?.start()
window?.makeKeyAndVisible()
@sagaya
sagaya / .swift
Created July 16, 2019 11:15
ssl_pining
let pathToCert = Bundle.main.path(forResource: "name-of-cert-file", ofType: "cer")
let localCertificate: NSData = NSData(contentsOfFile: pathToCert!)!
let serverTrustPolicy = ServerTrustPolicy.pinCertificates(
certificates: [SecCertificateCreateWithData(nil, localCertificate)!],
validateCertificateChain: true,
validateHost: true
)
let serverTrustPolicies = [