Last active
February 28, 2016 23:06
-
-
Save JohannMG/f93101678cea07444710 to your computer and use it in GitHub Desktop.
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
//: Playground - noun: a place where people can play | |
import UIKit | |
import XCPlayground | |
var str = "Hello, playground" | |
extension UIView { | |
} | |
extension CGFloat{ | |
func toFloat() -> Float{ | |
return Float(self) | |
} | |
} | |
class StartStopUIControl: UIControl { | |
enum ActionState { | |
case Recording | |
case Stopped | |
} | |
var actionlayer = CAShapeLayer() | |
var iconFrame: CGRect { | |
if action == .Stopped { | |
return CGRectInset(self.frame, recordIconPadding, recordIconPadding) | |
} | |
else { | |
return CGRectInset(self.frame, stopIconPadding, stopIconPadding) | |
} | |
} | |
var action:ActionState = ActionState.Recording | |
let stopIconPadding:CGFloat = 40 | |
let recordIconPadding: CGFloat = 17 | |
let stopButtonCornerRadius = CGFloat(10) | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setupActionIcon() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
setupActionIcon() | |
} | |
func setupActionIcon(){ | |
actionlayer = CAShapeLayer(layer: layer) | |
actionlayer.path = getIconForCurrentState().CGPath | |
actionlayer.frame = iconFrame | |
actionlayer.fillColor = UIColor.redColor().CGColor | |
actionlayer.strokeColor = UIColor.redColor().CGColor | |
layer.addSublayer(actionlayer) | |
} | |
func getIconForCurrentState() -> UIBezierPath{ | |
//if stopped, icon should should record action icon | |
if (action == .Stopped){ | |
return getRecordPath() | |
} else { | |
return getStopPath() | |
} | |
} | |
func getStopPath() -> UIBezierPath { | |
return UIBezierPath(roundedRect: CGRect( | |
x: 0, y: 0, | |
width: self.frame.width - (stopIconPadding * 2), | |
height: self.frame.height - (stopIconPadding * 2)), | |
cornerRadius: stopButtonCornerRadius) | |
} | |
func getRecordPath() -> UIBezierPath { | |
return UIBezierPath(ovalInRect: CGRect(x: 0, y: 0, | |
width: self.frame.width - (recordIconPadding * 2) , | |
height: self.frame.height - (recordIconPadding * 2))) | |
} | |
} | |
let outputView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150)) | |
outputView.backgroundColor = UIColor.grayColor() | |
let buttonView = StartStopUIControl(frame: outputView.frame) | |
buttonView.layer.cornerRadius = buttonView.frame.height/2 | |
buttonView.layer.borderColor = UIColor.whiteColor().CGColor | |
buttonView.layer.borderWidth = CGFloat(10) | |
outputView.addSubview(buttonView) | |
XCPlaygroundPage.currentPage.liveView = outputView | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment