Skip to content

Instantly share code, notes, and snippets.

View genedelisa's full-sized avatar
💭
Feeding the cats

Gene De Lisa genedelisa

💭
Feeding the cats
View GitHub Profile
@genedelisa
genedelisa / AlertController
Created August 11, 2014 19:07
UIAlertController in Swift
// ios8 and later
var alert = UIAlertController(title: "Hey!",
message: "How 'bout dat?",
preferredStyle: .Alert)
let textConfigClosure: ((UITextField!) -> Void)! = { text in
text.placeholder = "Type something here"
}
alert.addTextFieldWithConfigurationHandler(textConfigClosure)
@genedelisa
genedelisa / MIDISamplerSetup
Created August 12, 2014 12:03
Swift MIDI Sampler setup
class MIDISampler : NSObject {
var engine:AVAudioEngine!
var playerNode:AVAudioPlayerNode!
var mixer:AVAudioMixerNode!
var sampler:AVAudioUnitSampler!
override init() {
super.init()
initAudioEngine()
}
@genedelisa
genedelisa / Orientation
Created August 23, 2014 12:55
Receive device orientation notifications in Swift
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "detectOrientation",
name:UIDeviceOrientationDidChangeNotification,
object:nil)
return true
@genedelisa
genedelisa / webapp-arq.sh
Last active August 29, 2015 14:07
Bash script to generate project from maven archetype for Java EE7 webapp with Arquillian
#!/usr/bin/env bash
# maven archetype helper script
# Gene De Lisa
# === the variables
# your generated output
myartifact=sampleproject
mygroup=com.rockhoppertech
@genedelisa
genedelisa / git-bare.sh
Created October 16, 2014 13:15
Bash script to create a bare git repo then add remote to current directory
#!/usr/bin/env bash
# create a bare git repo
# Gene De Lisa
# === the variables
gitdir=~/Dropbox/git
# assumes you're in the project directory, so get the current dir name without the
# full path.
@genedelisa
genedelisa / Loop.swift
Created November 11, 2014 14:13
Looping an iOS/OSX AudioToolbox MusicTrack in Swift
func loopTrack(musicTrack:MusicTrack) {
var trackLength = getTrackLength(musicTrack)
println("track length is \(trackLength)")
setTrackLoopDuration(musicTrack, duration: trackLength)
}
func getTrackLength(musicTrack:MusicTrack) -> MusicTimeStamp {
//The time of the last music event in a music track, plus time required for note fade-outs and so on.
@genedelisa
genedelisa / StringLiteralConvertibleExample
Created February 2, 2015 10:57
Swift StringLiteralConvertible
public class Foo : StringLiteralConvertible {
var num:Int = 0
required public init(stringLiteral value: StringLiteralType) {
self.num = value.toInt()!
// of course you can do something more complicated by parsing the value.
}
public typealias UnicodeScalarLiteralType = StringLiteralType
@genedelisa
genedelisa / collectionViewScroll
Last active November 22, 2023 22:22
UICollectionView scroll to make section header visible
/**
Scroll to make the the given section header visible.
The function scrollToItemAtIndexPath will scroll to the item and hide the section header.
Swift 3.
*/
func scrollToSection(_ section:Int) {
if let cv = self.collectionView {
let indexPath = IndexPath(item: 1, section: section)
if let attributes = cv.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath) {
@genedelisa
genedelisa / Autolayout.swift
Last active November 12, 2016 11:47
Programmatic autolayout constraints
// the frame size doesn't matter; autolayout will change it
var aButton = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
aButton.titleLabel?.text = "HEY"
aButton.opaque = true
aButton.backgroundColor = UIColor.yellow
view.addSubview(aButton)
aButton.translatesAutoresizingMaskIntoConstraints = false
let margins = view.layoutMarginsGuide
@genedelisa
genedelisa / Toolbar.swift
Last active November 12, 2016 11:46
Create buttons in a navigationController's UIToolbar using Swift 3
if let nav = navigationController {
nav.setToolbarHidden(false, animated: false)
// doesn't really set the button's tint
UIToolbar.appearance().tintColor = UIColor.black
// instead of setting the text color of each bar button
UIBarButtonItem.appearance().tintColor = UIColor.black