Skip to content

Instantly share code, notes, and snippets.

// Swift 2.0 port of Objective-C code: http://www.theappguruz.com/blog/onoff-flashlight-one-button-ios
// Usage: AVCaptureDevice.turnLight()
// It does not work simultaneously with system turn of the torch
import AVFoundation
extension AVCaptureDevice {
static func turnLight() {
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if ( device.isTorchModeSupported(AVCaptureTorchMode.On) && device.isTorchModeSupported(AVCaptureTorchMode.Off)) {
@alsedi
alsedi / Localization.swift
Last active October 8, 2015 10:06
Localization of String, Int, Float, etc (Swift)
import Foundation
// Usage "String".localize will search for "String" key in Localizeble
// Works for all types that conform CustomStringConvertible protocol, e.g. Int, Float.
// Raw value will be converted to string and used as key, e.g.
// 10.localize, will search for "10" key.
// If key not exists, then string value will be returned
// Gist: https://gist.github.com/alsedi/357c99ff6d138cf94804
extension CustomStringConvertible {
@alsedi
alsedi / Wrapper.swift
Created October 22, 2015 12:15
Simple Class wrapper for Swift structures. Useful for NSNotificationCenter, for example
// Wrapper.swift
// Wrap Swift structure into the class. Useful in some operations with NSFoundation classes,
// e.g. NSNotificationCenter.postNotificationName
// Gist: https://gist.github.com/alsedi/6c8f5f0e313ea490aa69
class Wrapper<T> {
private var value: T
init(_ value: T) { self.value = value }
func unwrap() -> T { return self.value }
}
@alsedi
alsedi / Color.swift
Created November 15, 2015 19:26
Shorthand to create UIColor from Int values 0-255
import Foundation
import UIKit
extension UIColor {
static func RGBA(r:Int, _ g: Int, _ b:Int, _ a:Int) -> UIColor {
return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(a)/100.0)
}
static func RGB(r:Int, _ g: Int, _ b:Int) -> UIColor {
return UIColor.RGBA(r, g, b, 100)
}
@alsedi
alsedi / UIView+Contstraints.swift
Created March 16, 2016 20:31
Easy access to constraints by Identifier
//
// UIView+Contstraints.swift
//
// HOW TO USE
// 1. Setup constraints and define NSLayoutConstraint.identifier
// 2. Use identifier as parameter for call
// 3. Call on superview: if let constraint = self.view.constraintByStringId("Id") { }
// 4. Change, animate or replace constraint
import Foundation
@alsedi
alsedi / CircleToRectangle-Step-2.swift
Last active September 1, 2016 07:55
Code for tutorial at blog.alsedi.com
func animate() {
if shapes.count == 0 {
generateShapes()
}
// Animation
}
private func generateShapes() {
// Generate shapes todo
}
private var circlePath: CGPath {
let circle = UIBezierPath(ovalIn: CGRect(origin: CGPoint.zero, size: frame.size))
circle.close()
// 1
  return circle.cgPath
}
private var rectanglePath: CGPath {
let width = frame.size.width
let height = frame.size.height
let myPath = UIBezierPath()
myPath.lineCapStyle = CGLineCap.butt
myPath.lineJoinStyle = CGLineJoin.miter
myPath.move(to: CGPoint.zero)
myPath.addQuadCurve(to: CGPoint(x:width,y:0), controlPoint:CGPoint(x:width/2.0,y:0))
myPath.addQuadCurve(to: CGPoint(x:width,y:height), controlPoint:CGPoint(x:width,y:height/2.0))
func setup(size:CGSize, center:CGPoint, thickness: CGFloat) {
frame = CGRect(origin: CGPoint.zero, size: size)
position = center
path = circlePath
lineWidth = thickness
strokeColor = UIColor.black.cgColor
fillColor = UIColor.clear.cgColor
}
for i in 0..<shapesCount {
let shape = MutableShapeLayer()
shape.setup(size: minimalSize + (step * CGFloat(i)), center: center, thickness: shapeWidth)
shapes.append(shape)
layer.addSublayer(shape)
}