Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonathan Willing jwilling

View GitHub Profile
@jwilling
jwilling / gist:2292277
Created Apr 3, 2012
Drawing with shadow offset
View gist:2292277
- (void)drawInRect:(CGRect)rect {
CGSize shadowOffset = CGSizeMake(0, -1);
float colorValues[] = {0, 0, 0, .9};
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetShadow (ctx, shadowOffset, 1);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef color = CGColorCreate(colorSpace, colorValues);
@jwilling
jwilling / gist:2773202
Created May 23, 2012
Simple category for changing window size to fit content
View gist:2773202
- (void)resizeWindowForContentSize:(NSSize)size animated:(BOOL)animated {
NSRect windowFrame = [self contentRectForFrameRect:[self frame]];
NSRect newWindowFrame = [self frameRectForContentRect:
NSMakeRect( NSMinX( windowFrame ), NSMaxY( windowFrame ) - size.height, size.width, size.height )];
[self setFrame:newWindowFrame display:YES animate:animated];
}
@jwilling
jwilling / NSView+Animations.m
Created Jul 4, 2012
NSView add/removeSubview animated
View NSView+Animations.m
@implementation NSView (Animations)
- (void)addSubview:(NSView *)aView animated:(BOOL)animated {
[aView setAlphaValue:0.f];
[aView setFrameOrigin:NSZeroPoint];
CGFloat duration = animated ? (([[[self window] currentEvent] modifierFlags] & NSShiftKeyMask) ? 1.f : 0.25f ) : 0.f;
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:duration];
@jwilling
jwilling / JWLocationManager.h
Created Jul 31, 2012
Block-based wrapper around Core Location
View JWLocationManager.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
typedef void (^JWLocationManagerCallback)(CLLocation *location);
typedef void (^JWLocationManagerErrorCallback)(NSError *error);
@interface JWLocationManager : NSObject <CLLocationManagerDelegate>
@property (nonatomic, copy) NSString *purpose;
@property (nonatomic, copy) JWLocationManagerCallback locationUpdatedBlock;
@jwilling
jwilling / CustomClipView.m
Last active Oct 12, 2015
NSTableView with custom clip view
View CustomClipView.m
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (!self) return nil;
self.layer = [CAScrollLayer layer];
self.wantsLayer = YES;
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawNever;
return self;
}
@jwilling
jwilling / gist:4186817
Last active Apr 24, 2020
Lets help improve AppKit.
View gist:4186817

Is AppKit causing you frustration? Instead of just complaining about it, lets try to make it better by compiling a list of specific problems with AppKit. Leave a comment below, and I'll include it in the list.


##NSView##

  • NSView does not have the ability to set an affine transform (rdar://15608609)
  • Controls that have cells (such as NSTextField) do not respond properly when layer-backed. Take NSButton as an example. When not layer-backed, it will animate properly using the animator proxy. When layer-backed, using the animator proxy breaks focus rings (they jump to destination immediately). Currently, directly manipulating the layer of a cell-based control is not supported (and will lead to a broken interface), but is much needed. (rdar://15608822)

##NSViewController##

  • NSViewController could be more useful. It has -loadView but no other lifecycle methods. (rdar://15608948)
@jwilling
jwilling / gist:5929321
Last active Dec 19, 2015
Handling errors when using toProperty:onObject:.
View gist:5929321
RAC(self.someOrderedSet) = [[[[[[RACAble(self.searchField.text)
filter:^BOOL(NSString *text) {
return (text.length > 0);
}]
map:^id(NSString *text) {
return [Something searchForAutocomplete:text]; // this signal can send errors
}]
switchToLatest]
catchTo:[RACSignal return:[NSArray array]]]
map:^id(NSArray *objects) {
View JNWAsyncGroupOperation.m
#import <Foundation/Foundation.h>
typedef void (^JNWGroupAsyncOperationCompletion)();
@interface JNWGroupAsyncOperation : NSObject
// The completion block that will be fired when all operations have completed.
@property (nonatomic, copy) void(^groupCompletion)();
// Adds an asynchronous operation and starts it on the main thread. The completion block _must_
@jwilling
jwilling / JNWThrottledBlock.h
Last active Oct 26, 2020
Simple throttling of blocks using dispatch sources.
View JNWThrottledBlock.h
#import <Foundation/Foundation.h>
@interface JNWThrottledBlock : NSObject
// Runs the block after the buffer time _only_ if another call with the same identifier is not received
// within the buffer time. If a new call is received within that time period the buffer will be reset.
// The block will be run on the main queue.
//
// Identifier and block must not be nil.
+ (void)runBlock:(void (^)())block withIdentifier:(NSString *)identifier throttle:(CFTimeInterval)bufferTime;
@jwilling
jwilling / QTMAsyncOperationQueue.h
Created Oct 29, 2013
Simple block-based serial queue for asynchronous operations.
View QTMAsyncOperationQueue.h
//
// QTMAsyncOperationQueue.h
// Quantum
//
// Created by Jonathan Willing on 10/28/13.
// Copyright (c) 2013 AppJon. All rights reserved.
//
#import <Foundation/Foundation.h>
You can’t perform that action at this time.