Skip to content

Instantly share code, notes, and snippets.

View d-ronnqvist's full-sized avatar

David Rönnqvist d-ronnqvist

View GitHub Profile

Keybase proof

I hereby claim:

  • I am d-ronnqvist on github.
  • I am ronnqvist (https://keybase.io/ronnqvist) on keybase.
  • I have a public key whose fingerprint is 710B 9F42 55FC B0E9 DB06 9C2A 563F 6E00 ACB3 4F92

To claim this, I am signing this object:

@d-ronnqvist
d-ronnqvist / synthesize-readonly-in-subclass.m
Created July 14, 2014 09:13
Synthesizing a read-only property in a subclass overrides custom accessor
@interface Foo : NSObject
// some read-only property ...
@property (nonatomic, copy, readonly) NSString *bar;
@end
@implementation Foo
// ... with a custom accessor
- (NSString *)bar
{
return @"Foo";

The UIView animation API results in different CAAnimations being added to the layer on iOS 7 and 8. Changing the bounds of a view like this:

[UIView animateWithDuration:0.3 
                 animations:^{
    self.myView.bounds = CGRectMake(0, 0, 100, 100); // was (0,0) (200, 200)
}];

will result in one animation for the bounds key path being added to the backing layer if you are running iOS 7:

@d-ronnqvist
d-ronnqvist / gist:a70e6a1623bc7da85f1d
Last active August 29, 2015 14:10
The formatting horrors you sometimes need to go through to fit Objective-C in a two column layout
- (NSURL *) writeImage:(NSImage *)image
withSceneDocumentURL:(NSURL *)documentURL
originalImageURL:(NSURL *)originalImageURL
{
if (!originalImageURL) {
// Let the default exporter write this image
return nil;
}
NSString *sceneFileName =
[documentURL lastPathComponent];
@d-ronnqvist
d-ronnqvist / gist:7ea8b1dc7d10f23917c2
Created December 10, 2014 20:04
I'm designing something where there are multiple different objects that can hold exactly _one_ out of a couple of different other types, and I don't know what style I prefer.
// I'm designing something where there are multiple different objects that can hold
// exactly _one_ out of a couple of different other types, and I don't know what style I prefer.
//
// For example, an Input always has a name and it can hold a Symbol, a Target, or a Texture.
// It should always hold exactly one of them. In just the same way, a Symbol has a name and can
// hold different data of exactly exactly _one_ type of many.
//
// Which one of the following alternatives do you prefer?
@d-ronnqvist
d-ronnqvist / gist:22f40a2f52f48ccf7e89
Created May 7, 2015 12:40
Drawing a clipped gradient beyond the start and end points
import Foundation
import UIKit
class MyView: UIView {
override func drawRect(rect: CGRect) {
// just some path
let path = UIBezierPath(roundedRect: CGRectMake(10, 10, 200, 100), byRoundingCorners: .TopLeft | .BottomRight, cornerRadii: CGSizeMake(30, 60))
// fill with red
UIColor.redColor().setFill()
@d-ronnqvist
d-ronnqvist / TextHitTestingLabel.m
Last active December 19, 2015 03:29
The complete implementation of my answer to a StackOverflow question about how to hit test the actual text of a UILabel. http://stackoverflow.com/a/17379627/608157
//
// TextHitTestingLabel.m
// rotate
//
// Created by David Rönnqvist on 2013-06-29.
//
#import "TextHitTestingLabel.h"
#import <CoreText/CoreText.h>
@d-ronnqvist
d-ronnqvist / gist:6600444
Last active April 17, 2017 08:24
The purpose of this gist is to provide examples of many real life date computations This is uploaded as a gist so that it can be copy and pasted into either a OS X or iOS project. You can either take it all "[Cmd]+[A], [Cmd]+[C]" or just a small piece. The results are fairly well formattes as log output and I recommend that you run this code to …
// The purpose of this gist is to provide examples of many real life date computations
// This is uploaded as a gist so that it can be copy and pasted into either a OS X or
// iOS project. You can either take it all "[Cmd]+[A], [Cmd]+[C]" or just a small piece.
// The results are fairly well formattes as log output and I recommend that you run this
// code to see the actual results yourself. Change some of the values and see what happens.
#pragma mark - Setup
NSString *line = @"–––––––––––––––––––––––––––––––––––––––––––––––––––––––";
@d-ronnqvist
d-ronnqvist / gist:89768210611e18417b6d
Created October 19, 2014 15:47
Swift alternative to class clusters?

I wanted to use enums, structs, and classes to create small, elegant objects for a basic chess game.

It started with the small squares in the chess board, and created the logic part in the main definition and an extension that is use for rendering

enum Row: Character {
    case A = "A"
    case B = "B"
    case C = "C"
    case D = "D"
@d-ronnqvist
d-ronnqvist / Thoughts on removedOnCompletion in SparkRecordingCircle.md
Last active May 16, 2020 02:51
What I think is wrong with the Spark Recording Circle code and why

There was [a tweet][tweetSoto] a couple of days ago that resulted in a discussion/question about what it wrong with the usage of removedOnCompletion = NO in the [SparkRecordingCircle code][code] for that [Subjective-C post][post].

We all kept saying that the explanation doesn't fit in a tweet, so here is my rough explanation about the issues.

But, let me first say that I think that the Subjective-C articles are reallt cool and useful to learn from. This is trying to reason about the general usage of removedOnCompletion = NO, using that code as an example, since that was what was discussed on twitter.


The root problem, as [Nacho Soto pointed out][rootProblem], is that removedOnCompletion = NO in combination with fillMode = kCAFillModeForwards is almost always used when you are not updating the model layer. This means that after the animation has finished, what you see on screen is not reflected in the property of the layer. The biggest issue that this can cause is that all the