Skip to content

Instantly share code, notes, and snippets.

View matteodanelli's full-sized avatar
🎯
Focusing

Matteo matteodanelli

🎯
Focusing
  • Milan, Italy
  • 17:22 (UTC +02:00)
View GitHub Profile
var mySingleton = (function() {
var instance;
function init() {
// Private methods and variables
function privateMethod() {
console.log("I am private");
}
@matteodanelli
matteodanelli / UIWindowXT.swift
Created February 27, 2018 10:03
Get visible view controller
extension UIWindow {
func visibleViewController() -> UIViewController? {
if let rootViewController: UIViewController = self.rootViewController {
return UIWindow.getVisibleViewControllerFrom(vc: rootViewController)
}
return nil
}
class func getVisibleViewControllerFrom(vc:UIViewController) -> UIViewController {
@matteodanelli
matteodanelli / savePhotoToLibrary.swift
Created February 16, 2018 16:11
Save photos taken into library - iOS
// Info.plist requires "Privacy - Photo Library Additions Usage Description"
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
@matteodanelli
matteodanelli / Device.swift
Created January 31, 2018 16:24
Apple device detection_Swift
struct Device {
// iDevice detection code
static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad
static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
static let IS_RETINA = UIScreen.main.scale >= 2.0
static let SCREEN_WIDTH = Int(UIScreen.main.bounds.size.width)
static let SCREEN_HEIGHT = Int(UIScreen.main.bounds.size.height)
static let SCREEN_MAX_LENGTH = Int( max(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let SCREEN_MIN_LENGTH = Int( min(SCREEN_WIDTH, SCREEN_HEIGHT) )
@matteodanelli
matteodanelli / EdgeInsetLabel.swift
Created January 29, 2018 15:00
UILabelEdgeInsets
@IBDesignable
class EdgeInsetLabel: UILabel {
var textInsets = UIEdgeInsets.zero {
didSet { invalidateIntrinsicContentSize() }
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
let invertedInsets = UIEdgeInsets(top: -textInsets.top,
@matteodanelli
matteodanelli / selectors.swift
Created January 9, 2018 15:12
Swift selectors
fileprivate extension Selector {
static let buttonTapped = #selector(ViewController.buttonTapped)
static let deviceOrientationDidChange = #selector(ViewController.deviceOrientationDidChange)
}
// Example on how to use it
NotificationCenter.default.addObserver(self, selector: .deviceOrientationDidChange,
name: NSNotification.Name.UIDeviceOrientationDidChange,
object: nil)
@matteodanelli
matteodanelli / ImagePicker.swit
Last active October 21, 2020 17:47
Create iOS alert controller for image and video selection, from camera or filesystem
class ImagePicker: UIViewController, UINavigationControllerDelegate {
@IBOutlet var pictureView: UIImageView!
func tapAddPicture(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.mediaTypes = ["public.image", "public.movie"]
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { (action:UIAlertAction) in
@matteodanelli
matteodanelli / UIButtonXT.swift
Created November 4, 2017 14:42
Add wiggle animation to UIButton
import UIKit
extension UIButton {
// Animate a button, adding effect of "something went wrong". Useful for login button for example.
func wiggle() {
let wiggleAnim = CABasicAnimation(keyPath: "position")
wiggleAnim.duration = 0.05
wiggleAnim.repeatCount = 5
wiggleAnim.autoreverses = true
wiggleAnim.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
@matteodanelli
matteodanelli / CodeJamReader.swift
Created March 25, 2017 18:02
Google Code Jam stdin reader - Swift version
import Foundation
func readInput() {
let numberOfCases = Int(readLine() ?? "0")!
for _case in 0..<numberOfCases {
guard let currentLine = readLine() as String! else {
return
}
let lineValues = getIntegersOf(line: currentLine, separatedBy: " ")
@matteodanelli
matteodanelli / UITableViewDataSource.swift
Created January 24, 2017 14:51
UITableViewDataSource
override func numberOfSectionsInTableView(in tableView: UITableView) -> Int {
return NUMBER_OF_SECTIONS
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return NUMBER_OF_ELEMENTS_PER_SECTION
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_IDENTIFIER", for: indexPath)