Skip to content

Instantly share code, notes, and snippets.

View drance's full-sized avatar

Matt Drance drance

View GitHub Profile
@drance
drance / gist:71e347d2e2e29a1d8706
Created November 10, 2015 03:54
Lazy devs are lazy
alias gb="git branch"
alias gcb="git co -b"
alias gd="git difftool"
alias gl="git log"
alias gln="git log --name-only"
alias gs="git status"
alias gt="git tag -l"
alias gsl="git stash list"
alias gsms="git submodule sync"
alias gsmu="gsms; git submodule update --init --recursive"
@drance
drance / AppleTVName
Created November 3, 2014 20:45
Get the currently-routed AppleTV's name.
// Use in conjunction with e.g. KVO on AVPlayer.externalPlaybackActive
AVAudioSessionPortDescription *port = [AVAudioSession sharedInstance].currentRoute.outputs.firstObject;
if ([port.portType isEqualToString:AVAudioSessionPortAirPlay]) {
NSLog(@"Now playing on '%@'", port.portName);
}
@drance
drance / gist:d26278546acfaed54ba1
Created May 28, 2014 00:22
Run on the main thread. If we're already there, run synchronously.
@implementation NSOperationQueue (BHSAdditions)
+ (void)bhs_runOnMainQueue:(void (^)(void))block {
NSParameterAssert(block);
if ([NSThread isMainThread]) {
block();
} else {
[[self mainQueue] addOperationWithBlock:block];
}
}
@drance
drance / gist:9613950
Last active August 29, 2015 13:57
Got tired of semaphore/runloop boilerplate for async unit tests
- (void)sleepRunLoopForInterval:(NSTimeInterval)interval whileRunningAsynchronousTest:(void (^)(dispatch_semaphore_t semaphore))test {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSAssert((test != NULL), @"Passed a NULL test block");
test(semaphore);
while(dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
}
}
@drance
drance / gist:6317041
Created August 23, 2013 08:55
This NSOperationQueue category method lets you use a "cancelable" block with NSOperation. By passing the containing operation to the block, the block can return early or respond in other ways if the operation's state changes. The vanilla -addOperationWithBlock: has no reference to the operation, so it's impossible to know, let alone leverage, it…
// Passes the new operation to block so it can check for cancellation and exit early
- (NSOperation *)bhs_addOperationWithCancelableBlock:(void (^)(NSOperation *))block {
NSBlockOperation *op = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakOp = op;
[op addExecutionBlock:^{
block(weakOp);
}];
[self addOperation:op];
return op;
}
@drance
drance / UIViewController+BHSContainment.m
Last active September 2, 2021 00:26
Tired of the UIViewController containment three-step. Adds a convenience param if the destination superview is not actually the parent VC's main view.
@implementation UIViewController (BHSContainment)
- (void)bhs_addChildViewController:(UIViewController *)child {
[self bhs_addChildViewController:child superview:self.view];
}
// Note this potentially forces view loads on both parent and child
// Not a problem if used in -viewDidLoad or later
// Use superview parameter with care. If it's not within the parent VC's hierarchy, you deserve to lose
@drance
drance / gist:4546014
Created January 16, 2013 09:57
Workaround to vanilla UICollectionView+UICollectionViewFlowLayout using non-integral origins, leading to blurry cells.
@implementation BHSCollectionViewFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *allAttrs = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes *attributes in allAttrs) {
attributes.frame = CGRectIntegral(attributes.frame);
}
return allAttrs;
}
@drance
drance / ScrollieScrollView.m
Created December 13, 2011 07:11
PoC for an accessory view that tracks scrolling a la Path
// ScrollieScrollView
// PoC for an accessory view that tracks scrolling
// Created by Matt Drance (@drance) on 12/12/11.
@interface ScrollieScrollView : UIScrollView ()
@property (strong, nonatomic) UIView *thumb;
@end
@implementation ScrollieScrollView