Skip to content

Instantly share code, notes, and snippets.

@xandrucea
Last active December 24, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xandrucea/6791973 to your computer and use it in GitHub Desktop.
Save xandrucea/6791973 to your computer and use it in GitHub Desktop.
format UIDatePicker if labels background white
----------------------------------------------
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzzzz"];
NSString *stringyDate = @"2014-06-02 10:10:00 +0000";
NSDate *helperDate = [dateFormatter dateFromString:stringyDate];
datePickerDueDate.date = helperDate;
stringyDate = @"2031-01-13 19:43:00 +0000";
helperDate = [dateFormatter dateFromString:stringyDate];
datePickerDueDate.date = helperDate;
represent two dimensional array in a single ( two for loops in just one )
-------------------------------------------------------------------------
NSMutableArray *elements = [NSMutableArray new];
for (int i = 0; i < kNumberOfElements * kNumberOfElements; i++) {
int x = i % kNumberOfElements;
int y = i / kNumberOfElements;
CGRect rectAnswer = CGRectMake(x * _fElementSize,
y * _fElementSize,
_fElementSize,
_fElementSize);
CrosswordSubRect *tempSubRect = _arrayDataModel[_iRow][_iColumn];
[tempSubRect setRectFrame:rectAnswer];
[tempSubRect setupCell];
[tempSubRect setCrosswordName:self.strCrosswordName];
[elements addObject:tempSubRect];
}
id element = elements[5 + 7 * kNumberOfElements]; //X:5 Y:7
Calculate timespan for method execution
---------------------------------------
NSDate *methodStart = [NSDate date];
// execute method
NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"Executiontime >>> %f", executionTime);
Un / archive objects with NSKeyedArchiver / NSKeyedUnarchiver
-------------------------------------------------------------
- Each object needs to include `NSCoding` inside the
header and initWithCoder, encodeWith inside the implementation
- (NSMutableDictionary *)getDataFromArray:(NSMutableArray *)arrayData{
NSMutableDictionary *dictFromArray = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [arrayData count]; i++) {
NSMutableArray *arraySub = arrayData[i];
for (int j = 0; j < [arraySub count]; j++) {
NSString *strKey = [NSString stringWithFormat:@"%d-%d", j, i];
dictFromArray[strKey] = [NSKeyedArchiver archivedDataWithRootObject:arraySub[j]];
}
}
return dictFromArray;
}
- (void)getArrayFromDictFromUserDefaults:(NSMutableDictionary *)dictData{
for (int i = 0; i < kNumberOfElements; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < kNumberOfElements; j++) {
NSString *strKey = [NSString stringWithFormat:@"%d-%d", j, i];
CrosswordSubRect *subrect =
[NSKeyedUnarchiver unarchiveObjectWithData:dictData[strKey]];
[subArray addObject:subrect];
}
[_arrayCrosswordData addObject:subArray];
}
}
Get parentViewController of UINavigationController
--------------------------------------------------
NSUInteger numberOfViewControllersOnStack = [self.navigationController.viewControllers count];
ViewController *parentViewController = self.navigationController.viewControllers[numberOfViewControllersOnStack - 2];
Class parentVCClass = [parentViewController class];
NSString *className = NSStringFromClass(parentVCClass);
Create a custom debug NSLog in xcode
------------------------------------
- set "preprocessor macros" ---> DEBUG=1
- insert ExtendNSLogFunctionallity classes into project
- #import "ExtendNSLogFunctionality.h" ---> .pch
to create a specific log follow this tutorial:
http://mobile.tutsplus.com/tutorials/iphone/customize-nslog-for-easier-debugging/
URL Schemes - Call other apps within an app
-------------------------------------------
- check if app available
if ([[UIApplication sharedApplication] canOpenURL:@"whatsapp://"]) //do something
CocoaPods stuff
---------------
- install cocoa pods on project
touch Podfile (in dir of xcodeproj)
nano Podfile
--> insert and save
platform :ios, '5.0'
pod '[FrameworkName]', '~> 0.2'
pod install
pod search [name]
pod install
- turning off warning messages, change Podfile and use pod install again
pod '[framework]', [version], :inhibit_warnings => true
Implement Facebook events
-------------------------
- install newest sdk with pod
- insert header #import <FacebookSDK/FacebookSDK.h>
- insert in applicationDidBecomeActive
[FBSettings setDefaultAppID:kAppIdFacebook];
[FBAppEvents activateApp];
- insert other events like
[FBAppEvents logEvent:@"MyEvent"];
Implement MMDrawer
------------------
- (void)viewDidLoad{
[super viewDidLoad];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
highScore = (HighScore *)[storyboard instantiateViewControllerWithIdentifier:@"HighScore"];
details = (Details *)[storyboard instantiateViewControllerWithIdentifier:@"Details"];
[self setCenterViewController:highScore];
[self setLeftDrawerViewController:details];
[self setShouldStretchDrawer:NO];
[self setRestorationIdentifier:@"HighScore"];
[self setMaximumLeftDrawerWidth:290.0];
[self setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
[self setCloseDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
}
Create a circle and add an animation
------------------------------------
- Define variables
CAShapeLayer *circle, *circle2;
CABasicAnimation *drawAnimation, *colorSwitch;
- Create Circles and add them to your view
int radius = _imageViewCircle.frame.size.width;
circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, radius, radius)
cornerRadius:radius].CGPath;
circle.position = CGPointMake(_imageViewCircle.frame.origin.x,
_imageViewCircle.frame.origin.y);
circle.fillColor = [[UIColor clearColor] CGColor];
circle.strokeColor = [UIColor colorFromHexString:kColorQuestionRight].CGColor;
circle.lineWidth = 4;
circle2 = [CAShapeLayer layer];
circle2.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, radius, radius)
cornerRadius:radius].CGPath; // Make a circular shape
circle2.position = CGPointMake(_imageViewCircle.frame.origin.x,
_imageViewCircle.frame.origin.y); // Center the shape in self.view
circle2.fillColor = [UIColor clearColor].CGColor; // Configure the apperence of the circle
circle2.strokeColor = [UIColor colorFromHexString:kColorQuestionWrong].CGColor;
circle2.lineWidth = 4;
// Add to parent layer
[self.view.layer addSublayer:circle2];
[self.view.layer addSublayer:circle];
- Define the animations and add them to the circles
float animationDuration = 2.0;
drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = animationDuration; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..
drawAnimation.removedOnCompletion = NO; // Remain stroked after the animation..
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
drawAnimation.delegate = (id) self;
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
colorSwitch = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
colorSwitch.duration = animationDuration; // "animate over 10 seconds or so.."
colorSwitch.repeatCount = 1.0; // Animate only once..
colorSwitch.removedOnCompletion = NO; // Remain stroked after the animation..
colorSwitch.fromValue = [NSNumber numberWithFloat:0.0f];
colorSwitch.toValue = [NSNumber numberWithFloat:1.0f];
colorSwitch.delegate = (id) self;
colorSwitch.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
// Add the animation to the circle
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
//[circle2 addAnimation:colorSwitch forKey:@"drawColorSwitch"];
------> TO SWITCH BETWEEN THE CIRCLES USE ANIMATION DELEGATE METHOD
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if(flag){ //if animation hasn't stopped correctly the switch won't happen again
if([circle animationForKey:@"drawCircleAnimation"] == anim){
[circle2 addAnimation:colorSwitch forKey:@"drawColorSwitch"];
circle.zPosition = 1;
circle2.zPosition = 2;
} else {
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
circle2.zPosition = 1;
circle.zPosition = 2;
}
}
}
tips build phases
-----------------
-fno-objc-arc // allow arc
-w //surpress warnings
resize image to fit scale
-------------------------
resizeFactor = 1000.0f / 666.0f;
screenWidth = [UIScreen mainScreen].bounds.size.width;
screenHeight = [UIScreen mainScreen].bounds.size.height;
float imageWidth = screenHeight * resizeFactor;
[self.imageViewSplash setFrame:CGRectMake((-1 * (imageWidth / screenWidth / 2)), 0,
imageWidth, screenHeight)];
sorting an array
---------------
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW5
button alignment and managing subviews
--------------------------------------
[_buttonCommercial setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[_buttonCommercial setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
[_buttonCommercial insertSubview:badge belowSubview:_buttonCommercial.titleLabel];
INFO: If UIView is used as a subview, the button doesn't get the touch events anymore.
stop userinteraction completely
-------------------------------
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
create UIDocumentInteractionController
--------------------------------------
@property (strong, nonatomic) UIDocumentInteractionController *documentInteractionController;
UIButton *button = (UIButton *)sender;
[NSString writeStringToFile:_textView.text];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"text" withExtension:@"txt"];
NSLog(@"url: %@", url);
if(url){
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
[self.documentInteractionController setDelegate:self];
[self.documentInteractionController presentOptionsMenuFromRect:[button frame] inView:self.view animated:YES];
}
-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
open another app inside ios app
-------------------------------
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment