Skip to content

Instantly share code, notes, and snippets.

@mbuff24
Created October 10, 2014 20:17
Show Gist options
  • Save mbuff24/f2b473f1c76b798ae644 to your computer and use it in GitHub Desktop.
Save mbuff24/f2b473f1c76b798ae644 to your computer and use it in GitHub Desktop.
Making a capture style button with a UIView and 2 CALayers
class MainController: UIViewController {
@IBOutlet weak var captureButton: UIView?
let BORDER_WIDTH:CGFloat = 4
let RADIUS_MULTIPLIER:CGFloat = 0.5
let BUTTON_COLOR = UIColor.redColor().CGColor
let BUTTON_PRESSED_COLOR = UIColor.whiteColor().CGColor
let INNER_BUTTON_OFFSET = 0.4
var outerBtnLayer:CALayer = CALayer()
var innerBtn:CALayer = CALayer()
override func viewDidLoad() {
let buttonWidth = captureButton!.bounds.size.width;
let centerPoint:CGPoint = CGPointMake(buttonWidth / 2, buttonWidth / 2)
outerBtnLayer.bounds = captureButton!.frame
outerBtnLayer.borderColor = BUTTON_COLOR
outerBtnLayer.borderWidth = BORDER_WIDTH
outerBtnLayer.cornerRadius = buttonWidth * RADIUS_MULTIPLIER
outerBtnLayer.masksToBounds = true
outerBtnLayer.position = centerPoint;
captureButton?.layer.addSublayer(outerBtnLayer)
innerBtn.bounds = CGRect(x: buttonWidth * 0.2, y: buttonWidth * 0.2, width: buttonWidth * 0.8, height: buttonWidth * 0.8)
innerBtn.backgroundColor = BUTTON_COLOR
innerBtn.cornerRadius = innerBtn.bounds.size.width * RADIUS_MULTIPLIER
innerBtn.masksToBounds = true
innerBtn.position = centerPoint;
captureButton?.layer.addSublayer(innerBtn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment