Skip to content

Instantly share code, notes, and snippets.

View SAllen0400's full-sized avatar

Sean Allen SAllen0400

View GitHub Profile
@SAllen0400
SAllen0400 / sortArray.swift
Created April 3, 2017 05:11
Swift Snippet #2
class Player {
let name: String
let highScore: Int
init(name: String, highScore: Int) {
self.name = name
self.highScore = highScore
}
}
@SAllen0400
SAllen0400 / buttonAnimationExtension.swift
Created March 15, 2017 00:04
Core Animation on UIButton Example
// Swift 3
extension UIButton {
func pulsate() {
let pulse = CASpringAnimation(keyPath: "transform.scale")
pulse.duration = 0.6
pulse.fromValue = 0.95
pulse.toValue = 1.0
@SAllen0400
SAllen0400 / sortByProperty.swift
Last active March 8, 2017 22:59
Sorting an array by an object property
// Swift 3
// Simple object example
class Phone {
let name: String
let serialNumber: Int
init(name: String, serialNumber: Int) {
self.name = name
@SAllen0400
SAllen0400 / longpressGestureLength.swift
Created February 16, 2017 04:47
Get length of Long Press Gesture
// Swift 3
// If you'd like to use the gestureDuration outside of the scope of the button press, create some variables.
var gestureStartTime: TimeInterval!
var gestureDuration: TimeInterval!
// IBAction for UILongPressGestureRecognizer
@IBAction func recordButtonHeld(_ sender: UILongPressGestureRecognizer) {
switch sender.state {
@SAllen0400
SAllen0400 / customSlider
Created February 7, 2017 02:42
Custom Slider
import Foundation
class BMSliderControl: UIControl {
var minimumValue: Double = 0.0 {
didSet {
updateLayerFrames()
}
}
@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 / 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 / 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 / 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 / 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