Skip to content

Instantly share code, notes, and snippets.

@alanf
Created August 2, 2015 18:14
Show Gist options
  • Select an option

  • Save alanf/623d75cc80861257e571 to your computer and use it in GitHub Desktop.

Select an option

Save alanf/623d75cc80861257e571 to your computer and use it in GitHub Desktop.
Simple Quantizer
//
// ViewController.swift
// quant
//
import Foundation
import UIKit
@objc class ViewController : UIViewController {
var bpm:NSTimeInterval = 100
var currentBeat = 0
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(60 / (self.bpm*16), target: self, selector: "timerFired", userInfo: nil, repeats: true)
}
@IBOutlet weak var segmentedControl: UISegmentedControl!
func timerFired() {
currentBeat += 1
currentBeat = currentBeat % segmentedControl.numberOfSegments
segmentedControl.selectedSegmentIndex = currentBeat
if currentBeat % 16 == 0 {
segmentedControl.tintColor = UIColor.redColor()
} else {
segmentedControl.tintColor = UIColor.purpleColor()
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let image = getImageWithColor(UIColor.grayColor(), size: CGSizeMake(28, 28))
segmentedControl.setImage(image, forSegmentAtIndex: segmentedControl.selectedSegmentIndex)
}
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
var rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment