Skip to content

Instantly share code, notes, and snippets.

View malcommac's full-sized avatar
👋
Nice to meet u

Daniele Margutti malcommac

👋
Nice to meet u
View GitHub Profile
@malcommac
malcommac / DMMultiDelegatesProxy forwardInvocation
Last active August 29, 2015 13:56
DMMultiDelegatesProxy forwardInvocation code
- (void)forwardInvocation:(NSInvocation *)invocation {
// check if method can return something
BOOL methodReturnSomething = (![[NSString stringWithCString:invocation.methodSignature.methodReturnType encoding:NSUTF8StringEncoding] isEqualToString:@"v"]);
// send invocation to the main delegate and use it's return value
if ([self.mainDelegate respondsToSelector:invocation.selector])
[invocation invokeWithTarget:self.mainDelegate];
// make another fake invocation with the same method signature and send the same messages to the other delegates (ignoring return values)
NSInvocation *targetInvocation = invocation;
@malcommac
malcommac / gist:4a3fb6d2fbd469dcadc6
Created February 7, 2015 15:10
DMScrollViewStack LayoutSubviews
- (void) layoutSubviews:(BOOL) aAnimated completion:(void (^)(void)) aCompletion {
void (^layoutBlock)(void) = ^void(void) {
static BOOL isScrollView;
static CGRect visibleRect;
static CGFloat currentOffset;
static CGRect idealFrame;
static CGRect subviewFrame;
visibleRect = CGRectMake(0.0f, self.contentOffset.y, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
currentOffset = 0.0f;
@malcommac
malcommac / resample.swift
Created October 15, 2015 10:07
Resample Points
private static func resample(points: [StrokePoint], totalPoints: Int) -> [StrokePoint] {
var initialPoints = points
let interval = StrokePoint.pathLength(initialPoints) / Double(totalPoints - 1)
var totalLength: Double = 0.0
var newPoints: [StrokePoint] = [points.first!]
for var i = 1; i < initialPoints.count; ++i { let currentLength = initialPoints[i-1].distanceTo(initialPoints[i]) if ( (totalLength+currentLength) >= interval) {
let qx = initialPoints[i-1].x + ((interval - totalLength) / currentLength) * (initialPoints[i].x - initialPoints[i-1].x)
let qy = initialPoints[i-1].y + ((interval - totalLength) / currentLength) * (initialPoints[i].y - initialPoints[i-1].y)
let q = StrokePoint(x: qx, y: qy)
newPoints.append(q)
@malcommac
malcommac / IndicativeAngle.swift
Created October 15, 2015 10:13
Indicative Angle
public static func indicativeAngle(points: [StrokePoint]) -> Double {
let centroid = StrokePoint.centroid(points)
return atan2(centroid.y - points.first!.y, centroid.x - points.first!.x)
}
@malcommac
malcommac / RotatePoints.swift
Created October 15, 2015 10:13
Rotate Points
public static func rotate(points: [StrokePoint], byRadians radians: Double) -> [StrokePoint] {
let centroid = StrokePoint.centroid(points)
let cosvalue = cos(radians)
let sinvalue = sin(radians)
var newPoints: [StrokePoint] = []
for point in points {
let qx = (point.x - centroid.x) * cosvalue - (point.y - centroid.y) * sinvalue + centroid.x
let qy = (point.x - centroid.x) * sinvalue + (point.y - centroid.y) * cosvalue + centroid.y
newPoints.append(StrokePoint(x: qx, y: qy))
}
@malcommac
malcommac / ScaleAndTranslate.Swift
Created October 15, 2015 10:14
Scale & Translate Points
public static func scale(points: [StrokePoint], toSize size: Double) -> [StrokePoint] {
let boundingBox = StrokePoint.boundingBox(points)
var newPoints: [StrokePoint] = []
for point in points {
let qx = point.x * (size / boundingBox.width)
let qy = point.y * (size / boundingBox.height)
newPoints.append(StrokePoint(x: qx, y: qy))
}
return newPoints
}
@malcommac
malcommac / DistanceAtBestAngle.swift
Created October 15, 2015 10:14
Distance At Best Angle
public static func distanceAtBestAngle(points: [StrokePoint], strokeTemplate: [StrokePoint], var fromAngle: Double, var toAngle: Double, threshold: Double) -> Double {
var x1 = StrokeConsts.Phi * fromAngle + (1.0 - StrokeConsts.Phi) * toAngle
var f1 = StrokePoint.distanceAtAngle(points, strokeTemplate: strokeTemplate, radians: x1)
var x2 = (1.0 - StrokeConsts.Phi) * fromAngle + StrokeConsts.Phi * toAngle
var f2 = StrokePoint.distanceAtAngle(points, strokeTemplate: strokeTemplate, radians: x2)
while ( abs(toAngle-fromAngle) > threshold ) {
if f1 < f2 {
toAngle = x2
@malcommac
malcommac / CLLocationManagerExample.swift
Last active May 25, 2021 03:31
CLLocationManager Dummy Example
import Foundation
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
var manager:CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
@malcommac
malcommac / OneShotLocation_SwiftLocation.swift
Last active February 6, 2017 13:09
SwiftLocation to get current user location
LocationManager.shared.observeLocations(.Block, frequency: .OneShot, onSuccess: { location in
// Your desidered CLLocation is here, at specified accuracy with auth request managed for you
}) { error in
// Something went wrong. error will tell you what
}
@malcommac
malcommac / SwiftLocation_LocationFinderViaIP.swift
Created May 1, 2016 09:28
Location Finder Via IP Address
LocationManager.shared.locateByIPAddress(onSuccess: { placemark in
// placemark is a valid CLPlacemark object
}) { error in
// something wrong has occurred; error will tell you what
}