Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
pyrtsa / Color.swift
Created January 9, 2015 08:21
Fun with default arguments in Swift initialisers
import UIKit
struct Color {
let color: UIColor
init(r: CGFloat = 0.0,
g: CGFloat = 0.0,
b: CGFloat = 0.0,
a: CGFloat = 1.0)
{
color = UIColor(red: r, green: g, blue: b, alpha: a)
@pyrtsa
pyrtsa / QuotedString.swift
Created January 23, 2015 14:50
Quoted strings in Swift
struct QuotedString : Printable {
var string: String
init(_ string: String) {
self.string = string
}
var description: String {
let backslash: UnicodeScalar = "\\"
let quote: UnicodeScalar = "\""
let hex: [UnicodeScalar] = ["0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "a", "b", "c", "d", "e", "f"]
@pyrtsa
pyrtsa / Semigroupy.swift
Last active December 28, 2015 19:40
Semigroupy grouping magic in Swift
// What people often ask DefaultDict for, could be done with just one
// extension method for Optional.
extension Optional {
mutating func mappend(z: Wrapped, _ f: (Wrapped, Wrapped) -> Wrapped) {
self = map {x in f(x, z)} ?? z
}
}
// --- Examples ------------------------------------------------------------
@inducer
inducer / map-example.py
Created December 21, 2012 16:29
Example of how to use ALLOC_HOST_PTR for alignment/faster transfers
import numpy as np
import numpy.linalg as la
import pyopencl as cl
import pyopencl.array as cl_array
from time import time
KERNEL = """
@shepting
shepting / YMKeyboardLayoutHelperView.m
Last active March 22, 2016 02:50
A great little helper for handling keyboard animations nicely. Just put this view at the bottom vertically of your views and it will move everything else up and down for you.
//
// YMKeyboardLayoutHelperView.m
// ios-chat
//
// Created by Steven Hepting on 7/17/13.
// Copyright (c) 2013 Yammer. All rights reserved.
//
#import "YMKeyboardLayoutHelperView.h"
#import "UIView+LayoutAdditions.h"
@hfossli-agens
hfossli-agens / gist:4676773
Created January 30, 2013 20:45
dispatch_after with repeat / loop
static void dispatch_repeated_internal(dispatch_time_t firstPopTime, double intervalInSeconds, dispatch_queue_t queue, void(^work)(BOOL *stop))
{
__block BOOL shouldStop = NO;
dispatch_time_t nextPopTime = dispatch_time(firstPopTime, (int64_t)(intervalInSeconds * NSEC_PER_SEC));
dispatch_after(nextPopTime, queue, ^{
work(&shouldStop);
if(!shouldStop)
{
dispatch_repeated_internal(nextPopTime, intervalInSeconds, queue, work);
}
#
# Set the build number to the current git commit count.
# If we're using the Dev scheme, then we'll suffix the build
# number with the current branch name, to make collisions
# far less likely across feature branches.
# Based on: http://w3facility.info/question/how-do-i-force-xcode-to-rebuild-the-info-plist-file-in-my-project-every-time-i-build-the-project/
#
git=`sh /etc/profile; which git`
appBuild=`"$git" rev-list --all |wc -l`
if [ $CONFIGURATION = "Debug" ]; then
struct Regex {
let pattern: String
let options: NSRegularExpressionOptions!
private var matcher: NSRegularExpression {
return NSRegularExpression(pattern: self.pattern, options: self.options, error: nil)
}
init(pattern: String, options: NSRegularExpressionOptions = nil) {
self.pattern = pattern
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *moreAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"More" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
// maybe show an action sheet with more options
[self.tableView setEditing:NO];
}];
moreAction.backgroundColor = [UIColor lightGrayColor];
UITableViewRowAction *blurAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Blur" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
[self.tableView setEditing:NO];
}];
@dlo
dlo / Auto-layout-keyboard-adjustment.md
Last active February 26, 2021 07:33
How to adjust a view's height with Auto Layout when a keyboard appears or disappears in iOS 7.

This gist outlines how to resize a view when a keyboard appears using Auto Layout (there are a bunch of code samples out there that manually adjust the view's frame, but that's just so 2013). The method I outline below works universally on both iPhone and iPad, portrait and landscape, and is pretty darn simple.

Setting Up

The first thing to do is to define our containing view controller, the view, and the bottom constraint that we'll use to adjust its size.

Here's HeightAdjustingViewController.h. We don't need to expose any public properties, so it's pretty bare.