Skip to content

Instantly share code, notes, and snippets.

View SAllen0400's full-sized avatar

Sean Allen SAllen0400

View GitHub Profile
@SAllen0400
SAllen0400 / screenSizeStructs.swift
Created December 25, 2016 21:15
Structs for Screen Sizes
struct ScreenSize {
static let SCREEN_WIDTH = UIScreen.main.bounds.size.width
static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}
struct DeviceTypes {
static let iPhone4 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
static let iPhone5 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
@SAllen0400
SAllen0400 / usingScreenSizeStruct.swift
Created December 25, 2016 21:23
Using Screen Size Struct
if DeviceTypes.iPhone5 || DeviceTypes.iPhone7Zoomed {
// Do any iPhone 5 (or iPhone7 Zoomed mode) specific code here
}
if DeviceTypes.iPhone7PlusStandard {
// Do any iPhone 7 Plus specific code here
}
@SAllen0400
SAllen0400 / progressBarAnimation.swift
Last active September 1, 2017 19:26
Animating Progress Bar
extension UIProgressView {
func animate(duration: Double) {
setProgress(0.01, animated: true)
UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
self.setProgress(1.0, animated: true)
}, completion: nil)
}
@SAllen0400
SAllen0400 / basicTernaryOperator.swift
Last active January 1, 2017 04:13
Basic Ternary Operator
// Bad way using if statement
func showMyView(bool: Bool) {
if bool == true {
UIView.animate(withDuration: 0.33, animations: {
self.myView.alpha = 1.0
})
} else {
UIView.animate(withDuration: 0.33, animations: {
self.myView.alpha = 0.0
@SAllen0400
SAllen0400 / filterArray.swift
Created January 22, 2017 22:17
Filter Duplicates out of array
// Swift 3
extension Array {
func filterDuplicates(includeElement: @escaping (_ lhs: Element, _ rhs: Element) -> Bool) -> [Element] {
var results = [Element]()
forEach { (element) in
@SAllen0400
SAllen0400 / filterArrayExample.swift
Last active February 3, 2017 05:04
Example of Filtering Duplicates from Array Swift
// Swift 3
func setupBeaconRegions() {
// Use the .filterDuplicates function on an array of beacons to create a new array of only unique UUID's
let beaconRegions: [iBeaconItem] = iBeacons.filterDuplicates { $0.uuid == $1.uuid && $0.uuid == $1.uuid }
// Iterate through new array that only contains unique UUIDs and start ranging those
for beacon in beaconRegions {
startRangingBeacon(beacon)
@SAllen0400
SAllen0400 / navBarLogo.swift
Created January 23, 2017 04:26
Show logo in iOS NavBar
// Swift 3
// The reason I'm passing a UINavigationController and UINavigationItem as parameters is so
// this method can be abstracted away from the view controller.
func showLogoInNavBar(_ navController: UINavigationController, navItem: UINavigationItem) {
let banner = UIImage(named: "logo-nav-bar")
let imageView = UIImageView(image: banner)
let bannerWidth = navController.navigationBar.frame.size.width
@SAllen0400
SAllen0400 / customSwitch
Last active February 7, 2017 02:37
Custom Switch
import UIKit
class SASwitch: UIView {
var backgroundView: UIView!
var beforeButton: UIButton!
var afterButton: UIButton!
var buttonWindow: UIView!
var beforeLabel: UILabel!
var afterLabel: UILabel!
@SAllen0400
SAllen0400 / customSegControl
Created February 7, 2017 02:40
Custom Segmented Control
import Foundation
import UIKit
@IBDesignable class BMSegmentedControl: UIControl {
private var labels = [UILabel]()
var thumbView = UIView()
var items: [String] = ["BEFORE", "NEITHER", "AFTER"] {
didSet {
@SAllen0400
SAllen0400 / customSlider
Created February 7, 2017 02:42
Custom Slider
import Foundation
class BMSliderControl: UIControl {
var minimumValue: Double = 0.0 {
didSet {
updateLayerFrames()
}
}