This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create the xib CustomView.xib and add the View class as CustomView (this one) | |
override func awakeAfter(using aDecoder: NSCoder) -> Any? { | |
if self.subviews.isEmpty { | |
let nib = UINib(nibName: "CustomView", bundle: nil) | |
let viewArray = nib.instantiate(withOwner: nil, options: nil) | |
guard let view = viewArray.first as? CustomView else { | |
return self | |
} | |
view.translatesAutoresizingMaskIntoConstraints = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UILabel { | |
/// Highlights a specific substring of the label's text with a desired color | |
/// Text must be set before calling this method. | |
/// | |
/// - Parameters: | |
/// - subString: The substring to change color | |
/// - color: The desired color | |
func highlight(subString: String, color: UIColor?) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIView { | |
func roundCorners(corners: UIRectCorner, radius: CGFloat) { | |
let path = UIBezierPath(roundedRect: bounds, | |
byRoundingCorners: corners, | |
cornerRadii: CGSize(width: radius, height: radius)) | |
let mask = CAShapeLayer() | |
mask.path = path.cgPath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://medium.com/@nimjea/grand-central-dispatch-in-swift-fdfdd8b22d52 | |
//https://medium.com/@wailord/a-high-level-primer-on-ios-concurrency-df3bca3a6993 | |
//https://cocoacasts.com/choosing-between-nsoperation-and-grand-central-dispatch/ | |
DispatchQueue.main.async { | |
// Perform your async code here | |
} | |
//Synced operations | |
let northZone = DispatchQueue(label: "perform_task_with_team_north") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Variation of this feature: https://www.swiftbysundell.com/posts/reusable-data-sources-in-swift | |
import UIKit | |
/// Keep a strong reference to this, since UITableView only uses a weak reference for it. Used only for simple table views fast rendering. | |
class TableViewCommonCellDataSource<Model>: NSObject, UITableViewDataSource { | |
/// Cell configuration | |
typealias CellConfigurator = (Model, UITableViewCell) -> Void | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIViewController { | |
private static var _myComputedProperty = [String:Bool]() | |
var myComputedProperty:Bool { | |
get { | |
let tmpAddress = String(format: "%p", unsafeBitCast(self, to: Int.self)) | |
return UIViewController._myComputedProperty[tmpAddress] ?? false | |
} | |
set(newValue) { | |
let tmpAddress = String(format: "%p", unsafeBitCast(self, to: Int.self)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MySwitch: UISwitch { | |
typealias DidTapSwitch = (WMSwitch) -> () | |
var didChangeState: DidTapSwitch? { | |
didSet { | |
if didChangeState != nil { | |
addTarget(self, action: #selector(self.switchStateDidChange(_:)), for: .valueChanged) | |
} else { | |
removeTarget(self, action: #selector(self.switchStateDidChange(_:)), for: .valueChanged) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Map | |
//Loops over a collection and applies the same operation to each element in the collection. | |
var numberArray = [1,2,3,4,5] | |
var newArray = numberArray.map( {(value:Int) -> Int in | |
return value*2 | |
}) | |
//or quicker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class CircleView: UIView { | |
var viewLoaded:Bool = false | |
var lineWidth:CGFloat = 10 | |
var animationDuration:Double = 5 | |
let circlePathLayer = CAShapeLayer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//In case we had to load a view from a xib | |
//In case we had a nib for the chart | |
override public func awakeAfter(using aDecoder: NSCoder) -> Any? { | |
let isPlaceHolder:Bool = self.subviews.count == 0 | |
if isPlaceHolder { | |
let lineChartView:MyLineChartView? = Bundle.main.loadNibNamed("LineChartViewNibName", owner: nil, options: nil)?.first as? MyLineChartView | |
lineChartView?.autoresizingMask = self.autoresizingMask; | |
lineChartView?.translatesAutoresizingMaskIntoConstraints = self.translatesAutoresizingMaskIntoConstraints; |