Skip to content

Instantly share code, notes, and snippets.

View srstanic's full-sized avatar

Srđan Stanić srstanic

View GitHub Profile
@srstanic
srstanic / UIView+ShowHide.swift
Last active November 22, 2017 10:53
Showing and hiding UIView
extension UIView {
func show(animated: Bool) {
self.isHidden = false
if animated {
UIView.animate(
withDuration: animationDuration,
animations: {
self.alpha = 1
}
@srstanic
srstanic / UIScrollView+scrollForDistance.swift
Created December 6, 2017 17:40
Animated scroll to left or right for distance
extension UIScrollView {
func scrollToLeft(for distance: CGFloat, withAnimationDuration animationDuration: TimeInterval = 0.3) {
var newOffset = self.contentOffset.x - distance
if newOffset < 0 {
newOffset = 0
}
UIView.animate(withDuration: animationDuration) {
self.contentOffset.x = newOffset
}
@srstanic
srstanic / TableViewModel.swift
Last active June 22, 2020 11:38
Generic implementation of UITableViewDataSource & UITableViewDelegate
import UIKit
extension UITableViewCell {
class var reuseIdentifier: String {
return "tableViewCell"
}
}
extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
import Foundation
typealias VoidVoid = () -> ()
protocol Repeating {
func startRepeating(withIntervalInSeconds: TimeInterval)
func stopRepeating()
@srstanic
srstanic / ModelState.swift
Last active June 22, 2020 11:26
ModelState and ModelStateUpdateNotifier
import Foundation
protocol ModelStateDelegate: class {
func onModelStateOutdated()
}
class ModelState {
weak var delegate: ModelStateDelegate?
init(updateNotification: Foundation.Notification.Name, itemId: String? = nil, outdated: Bool = true) {
@srstanic
srstanic / NakedTabBarController.swift
Last active July 26, 2021 10:51
A UITabBarController with the invisible tabbar.
class NakedTabBarContorller: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.isHidden = true
}
}
@srstanic
srstanic / Repeater.swift
Created December 2, 2018 11:02
Repeating blocks
import Foundation
protocol Repeating {
func startRepeating(withIntervalInSeconds: TimeInterval)
func stopRepeating()
}
@srstanic
srstanic / NibView.swift
Last active December 27, 2020 14:46
NibView
import UIKit
/// Extend `NibView` to create a reusable view in a XIB file.
/// The contents of the view are designed in a XIB file.
/// The `NibView` class is responsible for loading the
/// contents of the XIB file and setting it as its subview.
/// File's Owner in the XIB file needs to be set to the
/// view class extending the `NibView`. Any outlets defined
/// in that class need to be connected to the File's Owner.
open class NibView: UIView {
extension Optional where Wrapped == String {
var orEmpty: String {
switch self {
case .some(let value):
return value
case .none:
return ""
}
}
}
@srstanic
srstanic / UIImage+tinting.swift
Created November 16, 2019 16:08
UIImage+tinting.swift
extension UIImage {
func tinted(with color: UIColor) -> UIImage? {
let maskImage = cgImage
let bounds = CGRect(origin: .zero, size: size)
let renderer = UIGraphicsImageRenderer(size: size)
// QuartzCore origin .zero is bottom-left, while UIKit's is top-left.
let tintedButFlippedImage = renderer.image { context in
let cgContext = context.cgContext
cgContext.clip(to: bounds, mask: maskImage!)