Skip to content

Instantly share code, notes, and snippets.

View SergLam's full-sized avatar
😊
Try to make your code better!

Serhii Liamtsev SergLam

😊
Try to make your code better!
View GitHub Profile
@SergLam
SergLam / CustomVC.swift
Created January 7, 2021 20:16
SwiftUI previews for UIKit
import UIKit
final class CustomVC: UIViewController {
private lazy var contentView: CustomView = CustomView()
// MARK: - Life cycle
deinit {
NotificationCenter.default.removeObserver(self)
}
@SergLam
SergLam / WordsFlexView.swift
Created December 10, 2020 16:32
Hashtags colored view sample implementation using UIStackViews
import UIKit
protocol WordsFlexViewDelegate: class {
func didSelectWord(word: String, intId: Int, phraseView: WordsFlexView)
}
class WordsFlexView: UIView {
enum WordsFlexViewType {
case selectable
@SergLam
SergLam / Animation.md
Created December 4, 2020 10:40 — forked from JeOam/Animation.md
iOS Core Animation: Advanced Techniques, Part 1: The Layer Beneath

Author: https://www.cyanhall.com/

1. The Layer Tree

Core Animation's original name is Layer Kit

Core Animation is a compositing engine; its job is to compose different pieces of visual content on the screen, and to do so as fast as possible. The content in question is divided into individual layers stored in a hierarchy known as the layer tree. This tree forms the underpinning for all of UIKit, and for everything that you see on the screen in an iOS application.

In UIView, tasks such as rendering, layout and animation are all managed by a Core Animation class called CALayer. The only major feature of UIView that isn’t handled by CALayer is user interaction.

There are four hierarchies, each performing a different role:

@SergLam
SergLam / HitTestUIView.swift
Created November 16, 2020 16:14
Pass touches over view bounds
import UIKit
class HitTestUIView: UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if clipsToBounds || isHidden || alpha == 0 {
return nil
}
for subview in subviews.reversed() {
let subPoint = subview.convert(point, from: self)
@SergLam
SergLam / USAState.swift
Created November 2, 2020 12:23
USA States names + ISO 3166-2 codes
import Foundation
// https://en.wikipedia.org/wiki/List_of_U.S._state_and_territory_abbreviations
// https://en.wikipedia.org/wiki/ISO_3166-2
// NOTE: - Last two symbols from ISO 3166-2 state code
enum USAState: Int, CaseIterable {
case alabama = 1
case alaska = 2
@SergLam
SergLam / Links.swift
Created September 17, 2020 15:02
Collection view - self sizing cells!!!!
@SergLam
SergLam / FileWebService.swift
Created September 14, 2020 06:25 — forked from boboboa32/FileWebService.swift
Moya file download
import Moya
import SwiftyJSON
enum FileWebService {
case download(url: String, fileName: String?)
var localLocation: URL {
switch self {
case .download(let url, let fileName):
let fileKey: String = url.MD5 // use url's md5 as local file name
@SergLam
SergLam / SocketClient+Events.swift
Last active September 3, 2020 15:57
Singleton - notify many subscribers via weak objects set - inspired by article - https://nshipster.com/nshashtable-and-nsmaptable/
extension SocketClient {
enum Event: String, CaseIterable {
// Chat:
case sendMessage = "speak"
case typing = "typing"
case lastSeen = "last_seen"
case unreadConversations = "unread_conversations"
}
@SergLam
SergLam / WeakSet.swift
Last active September 3, 2020 15:57
Weak set of object - inspired by article - https://nshipster.com/nshashtable-and-nsmaptable/
import Foundation
/// The WeakSet is weak: References to objects in the collection are held weakly.
/// If there is no other reference to an object stored in the WeakSet, they can be garbage collected.
public class WeakSet<T>: Sequence, ExpressibleByArrayLiteral, CustomStringConvertible, CustomDebugStringConvertible {
private var objects = NSHashTable<AnyObject>.weakObjects()
public init(_ objects: [T]) {
for object in objects {
@SergLam
SergLam / XIBLocalizable.swift
Last active September 1, 2021 20:01
XIB + Storyboard localisation hack via @IBInspectable attribute
import Foundation
import UIKit
// MARK: - Localized
public protocol Localized {
var localized: String { get }
}
extension String: Localized {
public var localized: String {