Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Air-Craft / recursive_blocks.m
Last active August 29, 2015 14:05
Recursive blocks without compiler warning in #Objective-C #intermediate
void (^doIt)(id retryBlk);
@weakify(self);
doIt = ^(id retryBlk){
void (^retry)(id retryBlk) = retryBlk;
retry(retryBlk);
};
doIt(doIt);
@Air-Craft
Air-Craft / video_snapshot_ios.m
Created August 14, 2014 14:02
Grab a snapshot from a video file i#iOS #video #images #snapshot #AVFoundation #intermediate
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:self.recordedVideoFileURL options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime cmTime = CMTimeMake(time, 1);
CGImageRef imgRef = [gen copyCGImageAtTime:cmTime actualTime:NULL error:&err];
UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];
return img;
@Air-Craft
Air-Craft / hide_keyboard_resign_first_responder.m
Created August 6, 2014 14:06
Resign first responder / hide keyboard absolutely #ios #basic #editing #keyboard
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
@Air-Craft
Air-Craft / uibutton_right_image.m
Created August 4, 2014 14:30
UIButton with image on the right #UIButton #iOS #basic #alignment
button.imageEdgeInsets = UIEdgeInsetsMake(0., button.frame.size.width - (image.size.width + 15.), 0., 0.);
button.titleEdgeInsets = UIEdgeInsetsMake(0., 0., 0., image.size.width);
@Air-Craft
Air-Craft / clone_uilabel.m
Created August 1, 2014 16:05
Clone a UILabel in iOS #iOS #UILabel #copying #intermediate
// Context: A UILabel category/subclass. Otherwise replace "self"...
- (instancetype)clonedVersionWithText:(NSString *)text
{
// Warning: Not necessarily complete, but a good start ;)
// Size & position
UILabel *clonedLbl = [[UILabel alloc] initWithFrame:self.frame];
// Text
@Air-Craft
Air-Craft / ios_list_fonts.m
Created July 30, 2014 15:21
List all available fonts in your iOS App #iOS #fonts #coding #basic
for (NSString* family in [UIFont familyNames])
{
NSLog(@"%@", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(@" %@", name);
}
}
@Air-Craft
Air-Craft / UICollectionViewFlowLayoutCenterItem.m
Last active August 29, 2015 14:04 — forked from mmick66/UICollectionViewFlowLayoutCenterItem.m
Make UICollectionViewFlowLayout handle page sizes that are less than the width. #ios #UICollectionView #scrolling #carousel
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
// We're going to center the centermost item...
// Use the bounds to have the snap happen the minute drag stops. In other words use the current content offset (which = bounds.origin) instead of the proposed end offset (which takes velocity into account)
CGRect proposedRect = self.collectionView.bounds;
CGFloat proposedContentOffsetCenterX = proposedContentOffset.x + self.collectionView.bounds.size.width * 0.5f;
// Comment out if you want the collectionview simply stop at the center of an item while scrolling freely (ie take velocity into account)
@Air-Craft
Air-Craft / scrollview_decelerate_end.m
Created July 25, 2014 15:39
ScrollView willScrollToEnd simulation #ios #scrolling #physics
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
const CGFloat DECEL = 500; // pixels/second decrease in velocity per second. need to guess this
CGPoint vel = [self.panGestureRecognizer velocityInView:self.superview];
if (vel.y > 0) return;
CGFloat bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
CGFloat distanceToBottom = scrollView.contentSize.height -bottomEdge;
@Air-Craft
Air-Craft / UIScrollView-velocity.m
Created July 25, 2014 15:09
Get scrolling velocity from a UIScrollView or UICollectionView #ios #intermediate #scrolling #physics
CGPoint scrollVelocity = [self.panGestureRecognizer velocityInView:self.superview];
if (scrollVelocity.y > 0.0f)
NSLog(@"going down");
else if (scrollVelocity.y < 0.0f)
NSLog(@"going up");
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
println("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"