Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Air-Craft / uiview_dynamic_shadow_motion.m
Created April 30, 2014 09:07
Dynamically draw inner shadow and outer shadow on a transparent image, linked to device angle position #uiview #intermediate #coregraphics #drawing #shadows #parallax #coremotion #ios
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIColor *outerShadowColor = [UIColor.blackColor colorWithAlphaComponent:1.0];
UIColor *innerShadowColor = [UIColor.blackColor colorWithAlphaComponent:0.6];
// Calc the shadow attribs based on the delta rotation
CMQuaternion newRotation = _motionManager.deviceMotion.attitude.quaternion;
@Air-Craft
Air-Craft / map_linear_range.c
Created April 30, 2014 15:11
C function to map a value from one linear range to another #maths #c #intermediate
static float _MapRange(float inVal, float inMin, float inMax, float outMin, float outMax) {
return ( (inVal-inMin) / (inMax-inMin) * (outMax-outMin) + outMin );
}
static float _MapBilinearRange(float inVal, float inMin, float inMed, float inMax, float outMin, float outMed, float outMax)
{
if (inVal < inMed)
return ( (inVal-inMin) / (inMed-inMin) * (outMed-outMin) + outMin );
else
return ( (inVal-inMed) / (inMax-inMed) * (outMax-outMed) + outMed );
}
@Air-Craft
Air-Craft / catransition_anim.m
Created May 21, 2014 12:30
CATransition animations: Ripple Effect, Cube #animations #basic #coreanimation #ios
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.startProgress = 0.0;
animation.endProgress = 1.0;
animation.duration = [self transitionDuration:transitionContext];
[animation setTimingFunction: [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setType:@"cube" ];
// [animation setType:@"rippleEffect" ];
animation.subtype = kCATransitionFromRight;
// kCATransitionFade
@Air-Craft
Air-Craft / catransaction_animations_2ways.m
Created May 22, 2014 10:43
Explicit layer animation with completion callback - 2 ways #basic #CoreAnimaton #ios #animation #transforms
- (void)animateMethod1
{
// Not always needed for some reason...
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:_SBR_ANIM_STAGE2_TIME];
// not needed unless you have multiple animations happening with different settings
// [CATransaction begin];
@Air-Craft
Air-Craft / repeating_layer_animation.m
Created May 23, 2014 09:18
Repeating reversing CALayer position animation #CoreAnimation #animation #basics #ios
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.fillMode = kCAFillModeForwards;
anim.removedOnCompletion = NO;
anim.byValue = [NSValue valueWithCGPoint:CGPointMake(0, 10)];
anim.duration = 0.4;
anim.repeatCount = HUGE_VALF;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
anim.autoreverses = YES;
[self.layer addAnimation:anim forKey:@"transform"];
@Air-Craft
Air-Craft / ios_version_test.m
Created May 26, 2014 12:17
Check/test iOS version number #basic #ios #versioning
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// < iOS 7
} else {
// iOS 7+
}
@Air-Craft
Air-Craft / mixin.m
Created May 30, 2014 11:16
Mixin methods from one class into another #objective-c #advanced
+ (void)mixInto:(id)destination
{
unsigned int methodCount = 0;
Method* methods = class_copyMethodList(self, &methodCount);
for (int i = 0; i < methodCount; i++) {
Method m = methods[i];
SEL name = method_getName(m);
IMP imp = method_getImplementation(m);
@Air-Craft
Air-Craft / new_gist_file_0
Created May 30, 2014 11:18
Check iPhone version w/ pre-processor macros #ios #versioning #macros #basic
#ifdef __IPHONE_7_0
// ...
#else
// ...
#endif
@Air-Craft
Air-Craft / new_gist_file_0
Created June 3, 2014 09:08
Xcode Build Phase Run Script for flagging “TODO” etc with compiler warnings #compilation #debugging #xcode #scripting
KEYWORDS="\@todo|\@fixme|\@hack|TODO:|HACK:|FIXME:"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($KEYWORDS).*\$" | perl -p -e "s/($KEYWORDS)/ warning: \$1/i"
@Air-Craft
Air-Craft / objc-method-with-string.m
Created June 3, 2014 15:16
Call an Objective-C method via a string #objective-c #basic #dynamic
SEL s = NSSelectorFromString(selectorName);
[anObject performSelector:s];