Skip to content

Instantly share code, notes, and snippets.

@mluton
mluton / ImageZoomAutolayout.swift
Created October 11, 2020 21:39
Zoom and pan an image in a scroll view with Auto Layout
import UIKit
class ViewController: UIViewController {
let scrollView = UIScrollView()
let containerView = UIView()
let imageView = UIImageView(image: UIImage(named: "quita"))
override func viewDidLoad() {
super.viewDidLoad()
@mluton
mluton / vstack-even-spacing.swift
Last active September 16, 2020 03:44
SwiftUI: Evenly Space Items in VStack
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Dark Side of the Moon")
.frame(maxHeight: .infinity)
Text("Wish You Were Here")
.frame(maxHeight: .infinity)
Text("Animals")
.frame(maxHeight: .infinity)
Text("The Wall")
@mluton
mluton / swiftui-list-in-popover.swift
Created September 13, 2020 18:10
SwiftUI - List In Popover
struct ContentView: View {
@State private var showPopover: Bool = false
var body: some View {
Button(action: {
self.showPopover = true
}) {
Text("Albums")
}.popover(isPresented: $showPopover) {
ScrollView(.vertical, showsIndicators: true) {
@mluton
mluton / dispatch-after.m
Created October 12, 2016 04:37
Dispatch After
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.00 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// code
});
@mluton
mluton / background-notifications.m
Last active October 13, 2016 16:58
Respond to app background/foreground notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodName) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodName) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodName) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodName) name:UIApplicationWillResignActiveNotification object:nil];
@mluton
mluton / re-sign-ipa.sh
Last active April 19, 2016 17:24 — forked from idy/re-sign-ipa.sh
Use codesign re-sign swift app
#!/bin/sh
# Usage: ./sign.sh INPUT.ipa OUTPUT PROVISION.mobileprovision IDENTITY_NAME ENTITLEMENTS BUNDLEID
# Reading parameters
INPUT=$1
OUTPUT=$2.ipa
MOBILE_PROVISTION=$3
CER_NAME=$4
ENTITLEMENTS=$5
BUNDLEID=$6
@mluton
mluton / simple-json-request.m
Created September 11, 2015 18:25
Simple JSON URL Request
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.example.com/endpoint_%@.json", parameter]];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
}];
[dataTask resume];
@mluton
mluton / CachedDateFormatter.swift
Last active August 8, 2023 12:04
Swift class that instantiates and caches NSDateFormatter objects
class CachedDateFormatter {
static let sharedInstance = CachedDateFormatter()
var cachedDateFormatters = [String: NSDateFormatter]()
func formatterWith(#format: String, timeZone: NSTimeZone = NSTimeZone.localTimeZone(), locale: NSLocale = NSLocale(localeIdentifier: "en_US")) -> NSDateFormatter {
let key = "\(format.hashValue)\(timeZone.hashValue)\(locale.hashValue)"
if let cachedDateFormatter = cachedDateFormatters[key] {
return cachedDateFormatter
}
@mluton
mluton / gist:5666378
Created May 28, 2013 21:41
Simple single-tap gesture recognizer
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[tapRecognizer setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapRecognizer];
}
@mluton
mluton / gist:5479855
Created April 29, 2013 05:30
NSLog a CGRect
NSLog(@"newBannerViewFrame: %@", NSStringFromCGRect(newBannerViewFrame));