Skip to content

Instantly share code, notes, and snippets.

View lamprosg's full-sized avatar

Lampros Giampouras lamprosg

  • Athens, Greece
View GitHub Profile
@lamprosg
lamprosg / BackButtonEventVC.swift
Created November 4, 2019 12:10
(iOS) Back button event
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
guard let nav = self.navigationController,
nav.viewControllers.contains(self) else {
self.delegate?.cancelledl()
return
}
}
@lamprosg
lamprosg / TitleWithRightImageButton.swift
Created October 18, 2019 13:22
(iOS) Button with left title and right image
class SomeButton: UIButton {
override func awakeFromNib() {
super.awakeFromNib()
self.semanticContentAttribute = UIApplication.shared
.userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
let insetAmount: CGFloat = 4.0
imageEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount)
titleEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount)
@lamprosg
lamprosg / StackView.swift
Created October 18, 2019 12:46
(iOS) StackView
private func renderData() {
let subviews = (0..<data.count).map { (index: Int) -> Someview in
let nib = UINib(nibName: "Someview", bundle: nil)
let viewArray = nib.instantiate(withOwner: nil, options: nil)
guard let view = viewArray.first as? Someview else {
return Someview()
}
@lamprosg
lamprosg / CircularProgressBar.swift
Created October 13, 2019 12:21
(iOS) Circular progress bar - UIView subclass
import Foundation
import UIKit
class CountdownProgressBar: UIView {
private var timer = Timer()
private var duration = 5.0
private var remainingTime = 0.0
private var showPulse = false
@lamprosg
lamprosg / UIView+Blur.swift
Created October 13, 2019 10:49
(iOS) Blur a UIView
import UIKit
private struct BlurableKey {
static var blurable = "blurable"
}
extension UIView {
func blur(blurRadius : CGFloat) {
if self.superview == nil {
@lamprosg
lamprosg / 0. Reference
Last active November 10, 2021 15:02
(iOS) SwiftUI tutorial
//https://www.hackingwithswift.com/quick-start/swiftui
//https://github.com/SimpleBoilerplates/SwiftUI-Cheat-Sheet
// Textfield with floating label
// https://medium.com/swlh/simpler-better-floating-label-textfields-in-swiftui-24f7d06da8b8
@lamprosg
lamprosg / wrappers.swift
Last active September 26, 2019 09:18
(iOS) Property wrappers
/*
A property wrapper adds a layer of separation between code
that manages how a property is stored and the code that defines a property
*/
//https://docs.swift.org/swift-book/LanguageGuide/Properties.html
//To define a property wrapper, you make a structure, enumeration, or class that defines a wrappedValue property
//wrappedValue can be anything, even generic value of the struct
@propertyWrapper
struct SmallNumber {
@lamprosg
lamprosg / 1.SequenceExample.swift
Last active September 18, 2020 01:53
(iOS) Conforming to Sequence & Collection protocols
//A sequence is a list of values that you can step through one at a time.
//The most common way to iterate over the elements of a sequence is to use a for-in loop:
/*
Potentially our database could contain a large set of records,
and for each model we need to hit the disk to actually load its data,
so we don’t want to load everything at once.
To make this happen, we’re going to replace the array return type with our own custom sequence.
*/
//https://www.swiftbysundell.com/articles/swift-sequences-the-art-of-being-lazy/
@lamprosg
lamprosg / (iOS) LinkedList.swift
Last active September 10, 2019 10:22
Creating a linked list
//https://swiftbysundell.com/articles/picking-the-right-data-structure-in-swift/
/*
A list struct, which will keep track of the first and last nodes within our list
Both of those properties are read-only outside of our type, to be able to ensure data consistency
*/
struct List<Value> {
private(set) var firstNode: Node?
private(set) var lastNode: Node?
}
@lamprosg
lamprosg / Caching.swift
Last active January 1, 2020 02:41
(iOS) Caching
// To be able to use strings as caching keys, we have to use
// NSString here, since NSCache is only compatible with keys
// that are subclasses of NSObject:
let cache = NSCache<NSString, MyClass>()
//Problem:
//can only store class instances, and it’s only compatible with NSObject-based keys
//NSCache simple use here:
//https://www.hackingwithswift.com/example-code/system/how-to-cache-data-using-nscache