Skip to content

Instantly share code, notes, and snippets.

View mansi-27's full-sized avatar
🎯
Focusing

Mansi Shah mansi-27

🎯
Focusing
View GitHub Profile
if FileManager.default.fileExists(atPath: fileURL.path) {
do {
let data = try Data(contentsOf: fileURL)
/// Sweetie, this is the data you need to upload as the multipart form data
/// via URLSession or Alamofire, Moya, whatever Network Library you're using! 🍻
} catch {
print(error.localizedDescription)
}
}
extension MyViewController: UIDocumentMenuDelegate, UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
/// Handle your document
}
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .overCurrentContext
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
import UIKit
class ViewController: UIViewController {
/// Creating UIDocumentInteractionController instance.
let documentInteractionController = UIDocumentInteractionController()
override func viewDidLoad() {
super.viewDidLoad()
/// Setting UIDocumentInteractionController delegate.
@mansi-27
mansi-27 / UIView+Extensions
Created February 22, 2018 18:02
Produce fade-in fade-out animation based on view's alpha
extension UIView {
/// Produce fade in animation
func fadeIn(_ duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion)
}
/// Produce fadeout animation
@mansi-27
mansi-27 / UIView+Extensions
Created February 22, 2018 18:02
Sets border to the UIView
extension UIView {
/// Sets border to the UIView
func setViewBorder(withWidth width: CGFloat = 1.0, color: UIColor) {
self.layer.borderWidth = width
self.layer.borderColor = color.cgColor
}
}
@mansi-27
mansi-27 / UIView+Extensions
Created February 22, 2018 18:01
Produce vertical shake animation
extension UIView {
/// Produce vertical shake animation
func shakeVertically(withCount count: Float = 4, duration: TimeInterval = 0.3, translation: Float = -10) {
let animation: CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.y")
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.repeatCount = count
animation.duration = (duration)/TimeInterval(animation.repeatCount)
animation.autoreverses = true
@mansi-27
mansi-27 / UIView+Extensions
Created February 22, 2018 18:00
Sets rounded corners to the UIView
extension UIView {
/// Sets rounded corners to the UIView
func roundedCorners(radius: CGFloat? = nil) {
if let radius = radius {
layer.cornerRadius = radius
} else {
layer.cornerRadius = frame.size.height / 2
}
clipsToBounds = true
@mansi-27
mansi-27 / UIView+Extensions
Last active April 30, 2018 07:18
Produce horizontal shake animation
extension UIView {
/// Produce horizontal shake animation
func shakeHorizontally(withCount count: Float = 4, duration: TimeInterval = 0.3, translation: Float = -10) {
let animation: CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.repeatCount = count
animation.duration = (duration)/TimeInterval(animation.repeatCount)
animation.autoreverses = true
func setScaledCustomFont() {
myLabel.font = getScaledFont(forFont: "Cambria-Bold", textStyle: .title1)
myLabel.adjustsFontForContentSizeCategory = true
}
/// Get the Scaled version of your UIFont.
///
/// - Parameters:
/// - name: Name of the UIFont whose scaled version you wish to obtain.
/// - textStyle: The text style for your font, i.e Body, Title etc...
/// - Returns: The scaled UIFont version with the given textStyle
func getScaledFont(forFont name: String, textStyle: UIFontTextStyle) -> UIFont {
/// Uncomment the code below to check all the available fonts and have them printed in the console to double check the font name with existing fonts 😉