全系列文章索引:
1 計(jì)算指定時間與當(dāng)前的時間差
計(jì)算指定時間與當(dāng)前的時間差
http://blog.csdn.net/xinshou_jiaoming/article/details/7068328
????計(jì)算指定時間與當(dāng)前的時間差?比如肩碟,3天前强窖、10分鐘前(這個在項(xiàng)目中經(jīng)常遇到,所以記錄了下來)
以下是實(shí)現(xiàn)方法:
/**
?*計(jì)算指定時間與當(dāng)前的時間差
?*@paramcompareDate ? 某一指定時間
?*@return 多少(秒or分or天or月or年)+前 (比如削祈,3天前翅溺、10分鐘前)
*/
+(NSString *) compareCurrentTime:(NSDate*) compareDate
{
????NSTimeInterval? timeInterval = [compareDate timeIntervalSinceNow];
????timeInterval = -timeInterval;
????long temp = 0;
? ? ?NSString*result;
????if (timeInterval < 60) {
? ? ? result = [NSStringstringWithFormat:@"剛剛"];
????}
????else if((temp = timeInterval/60) <60){
? ? ????result = [NSStringstringWithFormat:@"%d分前",temp];
????}
????else if((temp = temp/60) <24){
? ? ? result = [NSStringstringWithFormat:@"%d小前",temp];
????}
? else if((temp = temp/24) <30){
? ? ? result = [NSStringstringWithFormat:@"%d天前",temp];
????}
? else if((temp = temp/30) <12){
? ? ? result = [NSStringstringWithFormat:@"%d月前",temp];
????}
? else{
? ? ? temp = temp/12;
? ? ? result = [NSStringstringWithFormat:@"%d年前",temp];
????}
? return result;
}
以下是NSDate中的常用方法:
/**
- (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate*)refDate;
??初始化為以refDate為基準(zhǔn),然后過了secs秒的時間
- (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
??初始化為以當(dāng)前時間為基準(zhǔn)髓抑,然后過了secs秒的時間
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;
??以refDate為基準(zhǔn)時間咙崎,返回實(shí)例保存的時間與refDate的時間間隔
- (NSTimeInterval)timeIntervalSinceNow;
??以當(dāng)前時間(Now)為基準(zhǔn)時間,返回實(shí)例保存的時間與當(dāng)前時間(Now)的時間間隔
- (NSTimeInterval)timeIntervalSince1970;
??以1970/01/01 GMT為基準(zhǔn)時間吨拍,返回實(shí)例保存的時間與1970/01/01 GMT的時間間隔
- (NSTimeInterval)timeIntervalSinceReferenceDate;
??以2001/01/01 GMT為基準(zhǔn)時間褪猛,返回實(shí)例保存的時間與2001/01/01 GMT的時間間隔
+ (NSTimeInterval)timeIntervalSinceReferenceDate;
*/
??//秒
// - (NSTimeInterval)timeIntervalSinceNow;
//? ??以當(dāng)前時間(Now)為基準(zhǔn)時間,返回實(shí)例保存的時間與當(dāng)前時間(Now)的時間間隔
2? IOS開發(fā)常用數(shù)學(xué)函數(shù)
1羹饰、?三角函數(shù)
double sin (double);正弦
double cos (double);余弦
double tan (double);正切
2 伊滋、反三角函數(shù)
double asin (double); 結(jié)果介于[-PI/2,PI/2]
double acos (double); 結(jié)果介于[0,PI]
double atan (double); 反正切(主值), 結(jié)果介于[-PI/2,PI/2]
double atan2 (double,
double); 反正切(整圓值), 結(jié)果介于[-PI,PI]
3 、雙曲三角函數(shù)
double sinh (double);
double cosh (double);
double tanh (double);
4 队秩、指數(shù)與對數(shù)
double exp (double);求取自然數(shù)e的冪
double sqrt (double);開平方
double log (double); 以e為底的對數(shù)
double log10 (double);以10為底的對數(shù)
double pow(double x, double y);計(jì)算以x為底數(shù)的y次冪
float powf(float x, float y);功能與pow一致笑旺,只是輸入與輸出皆為浮點(diǎn)數(shù)
5 、取整
double ceil (double); 取上整
double floor (double); 取下整
6 馍资、絕對值
double fabs (double);求絕對值
double cabs(struct complex znum) ;求復(fù)數(shù)的絕對值
7 筒主、標(biāo)準(zhǔn)化浮點(diǎn)數(shù)
double frexp (double f, int*p); 標(biāo)準(zhǔn)化浮點(diǎn)數(shù), f = x * 2^p, 已知f求x,p ( x介于[0.5,1] )
double ldexp (double x, int p); 與frexp相反, 已知x,p求f
8 、取整與取余
double modf (double, double*); 將參數(shù)的整數(shù)部分通過指針回傳, 返回小數(shù)部分
double fmod (double, double);返回兩參數(shù)相除的余數(shù)
9 迷帜、其他
double hypot(double x, double y);已知直角三角形兩個直角邊長度物舒,求斜邊長度
double ldexp(double x, int exponent);計(jì)算x*(2的exponent次冪)
double poly(double x, int degree, double coeffs [] );計(jì)算多項(xiàng)式
int matherr(struct exception *e);數(shù)學(xué)錯誤計(jì)算處理程序
轉(zhuǎn)載:http://blog.csdn.net/zyc851224/article/details/7843859
3 IOS獲取屏幕尺寸與分辨率
IOS獲取屏幕分辨率
http://blog.csdn.net/tangaowen/article/details/7597535
????????獲取屏幕分辨率是個很有用的功能色洞,尤其在一些游戲相關(guān)的開發(fā)中戏锹,圖形的繪制與屏幕分辨率密不可分。得到當(dāng)前屏幕的分辨率是必不可少的支持火诸。
????????獲取屏幕分辨率可以兩步走
1锦针、得到當(dāng)前屏幕的尺寸:
CGRect?rect_screen = [[UIScreenmainScreen]bounds];
CGSize?size_screen= rect_screen.size;
2、獲得scale:
CGFloat scale_screen = [UIScreenmainScreen].scale;
????????此時屏幕尺寸的寬高與scale的乘積就是相應(yīng)的分辨率值置蜀。
4? iPhone開發(fā)分辨率
1.iPhone5分辨率320x568奈搜,像素640x1136,@2x
2.iPhone6分辨率375x667盯荤,像素750x1334馋吗,@2x
3.iPhone6 Plus分辨率414x736,像素1242x2208秋秤,@3x
????????這里所注的都是已經(jīng)添加相關(guān)尺寸loading圖后的開發(fā)分辨率和像素數(shù)宏粤,其中iphone6 plus最終的物理分辨率會被蘋果自動縮放到1080p(縮放比例1.14)脚翘。
iPhone6分辨率與適配
http://www.cocoachina.com/ios/20140912/9601.html
5 【編譯】Cannotassign to 'self' outside of a method in the init family
????????有時候我們重寫父類的init方法時不注意將init后面的第一個字母寫成了小寫,在這個方法里面又調(diào)用父類的初始化方法(self = [super init];)時會報錯绍哎,錯誤信息如下:error:Cannot assign to 'self' outside of a methodin the init family
????????原因:只能在init方法中給self賦值来农,Xcode判斷是否為init方法規(guī)則:方法返回id,并且名字以init+大寫字母開頭+其他? 為準(zhǔn)則崇堰。例如:-(id) initWithXXX;
出錯代碼:
-(id) Myinit{
????self = [super init];
????……
}
解決方法:
-(id) initWithMy
{
????self = [super init];
}
如下代碼:
?????? 僅僅因?yàn)榇笮憜栴}沃于,將initWithDelegate寫成了-(id) initwithDelegate,就會報錯
6 CoreData臨時實(shí)體對象
NSEntityDescription *entity = [NSEntityDescription entityForName:NSStringFromClass([IMUserInfoEntity class]) inManagedObjectContext:[[IMDataModelCoreDataStorage shareInstance] mainThreadManagedObjectContext]];
??? IMUserInfoEntity *userTmpEntity = [[IMUserInfoEntity alloc] initWithEntity: entity insertIntoManagedObjectContext: nil];
7 【Storyboard】在Storyboard中添加子View后海诲,頁面控件不顯示問題
問題:
?????? 在Storyboard的一個ViewController中添加子View后繁莹,再在代碼中新建一個子View來替代此View,導(dǎo)致在代碼中添加按鈕控件特幔,按鈕不顯示蒋困,但是可以接收到按鈕事件。
//初始化地圖View
????if (!_bMapView) {
??????? _bMapView? = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_BOUNDS.size.width, SCREEN_BOUNDS.size.height * 0.05)];//300 [[BMKMapView alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)];
????}
? ? self.mapView = _bMapView;
??? if (![self.view.subviews containsObject:self.mapView]) {
??????? [self.view addSubview:self.mapView];
??? }
????UIButton *_provinceButton = [[UIButton alloc] initWithFrame:CGRectMake(120, 120, 100, 40)];
??? _provinceButton.titleLabel.text = @"TestButton";
??? _provinceButton.titleLabel.textColor = [UIColor yellowColor];
??? [_provinceButton addTarget: self action: @selector(TestprovinceButtonClicked:) forControlEvents: UIControlEventTouchUpInside];
??? [self.view addSubview: _provinceButton];
解決方案:
?????? 在Storyboard的ViewController中添加的子View已經(jīng)實(shí)例化了敬辣,通過簡單地替換操作雪标,不會使其實(shí)例自動釋放,因?yàn)橐呀?jīng)作為子View溉跃,添加進(jìn)ViewController所在的View中了村刨。需要手動先將其從SuperView移除,然后再重新添加新View的實(shí)例撰茎。
????//初始化地圖View
??? if (!_bMapView) {
??????? _bMapView? = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_BOUNDS.size.width, SCREEN_BOUNDS.size.height * 0.05)];//300 [[BMKMapView alloc]
initWithFrame:CGRectMake(0, 0, 320, 480)];
??? }
??? if (self.mapView) {
??????? [self.mapView removeFromSuperview];
??? }
? ? self.mapView = _bMapView;
??? if (![self.view.subviews containsObject:self.mapView]) {
??????? [self.view addSubview:self.mapView];
??? }
8 【控件】UIButton控件文字不顯示
//此行頁面不會顯示文字
_provinceButton.titleLabel.text = @"Test Button";
//此行頁面顯示文字
?[_provinceButtonsetTitle:@"Test1" forState:UIControlStateNormal];
9 【CoreData】like查詢
查詢不到結(jié)果寫法
//??? NSPredicate*predicate=[NSPredicate predicateWithFormat:@"province LIKE '%@?' AND cityLIKE '%@?' AND county =%@",tempEntity.province, tempEntity.city, tempEntity.county];
可查詢到結(jié)果寫法:
NSString *predStr = [NSString stringWithFormat:@"province LIKE \'%@?\' AND city LIKE \'%@?\' AND county = \'%@\'",tempEntity.province, tempEntity.city, tempEntity.county];
NSPredicate*predicate = [NSPredicate predicateWithFormat: predStr];
NSString * predStr = [NSString stringWithFormat:@"province LIKE \'%@%%\' AND city LIKE \'%@%%\' AND county = \'%@\'",tempEntity.province, tempEntity.city, tempEntity.county];
10 iOS字符串 中包含%百分號的方法
????百分號的轉(zhuǎn)換嵌牺,NSString中需要格式化的字符串中百分號使用%%表示,而char*中百分號也是使用%%表示龄糊。
????例如:NSLog(@"%%%@%%",@"hello"),控制臺會打印出%hello%逆粹。
11 【UILabel】自適應(yīng)高度和自動換行
//初始化label??
UILabel?*label?=?[[UILabel?alloc]?initWithFrame:CGRectMake(0,0,0,0)];??
//設(shè)置自動行數(shù)與字符換行??
[label?setNumberOfLines:0];??
label.lineBreakMode?=?UILineBreakModeWordWrap;???
//?測試字串??
NSString?*s?=?@"這是一個測試!l懦汀僻弹!adsfsaf時發(fā)生發(fā)勿忘我勿忘我勿忘我勿忘我勿忘我阿阿阿阿阿阿阿阿阿阿阿阿阿啊00000000阿什頓。他嚷。蹋绽。";??
UIFont?*font?=?[UIFont?fontWithName:@"Arial"?size:12];??
//設(shè)置一個行高上限??
CGSize?size?=?CGSizeMake(320,2000);??
//計(jì)算實(shí)際frame大小,并將label的frame變成實(shí)際大小??
CGSize?labelsize?=?[s?sizeWithFont: font?constrainedToSize: size?lineBreakMode: UILineBreakModeWordWrap];??
[label?setFrame:CGRectMake:(0, 0,?labelsize.width, labelsize.height)];??
IOS7以上做法
http://www.tuicool.com/articles/eYbAv2
UILabel自適應(yīng)高度和自動換行
http://blog.csdn.net/csj1987/article/details/6662852
iOS學(xué)習(xí)5:UILabel的使用
http://bbs.9ria.com/thread-244444-1-1.html
12 Mac上顏色提取工具
????????很多人有這個需求:把鼠標(biāo)放在一個點(diǎn)上筋蓖,顯示該點(diǎn)顏色的RGB值卸耘。其實(shí)蘋果電腦的Mac OS X系統(tǒng)就自帶鼠標(biāo)所在點(diǎn)顏色RGB值查看工具:數(shù)碼測色計(jì),只是藏得比較深罷了粘咖。打開Finder(Dock欄第一個笑臉圖標(biāo))蚣抗,選擇應(yīng)用程序--實(shí)用工具--數(shù)碼測色計(jì),雙擊即可啟動瓮下。
????????這個界面大家都能看懂了吧翰铡,中間是預(yù)覽鼠標(biāo)所處位置得像素设哗,右側(cè)顯示顏色RGB值,取點(diǎn)范圍大小可以通過滑動條來調(diào)節(jié)两蟀。
????????在數(shù)碼測色計(jì)得下拉菜單里選擇RGB數(shù)值模式密任,有“255,255,255”那種网缝,也有“0000FF”那種。
????OK,說完了硬霍,很簡潔的一個蘋果Mac OS X系統(tǒng)自帶工具庇麦,但很有用吻谋。嫌它“埋”得太深逝变,可以直接拖到上級得“應(yīng)用程序”目錄里(Mac的精華就是“想拖就拖”)。Enjoy your Mac
13 【Rect】CGRect比較函數(shù)
CGRectEqualToRect(tableRect, _flagshipStoreTableView.frame)
14? iOS將View的內(nèi)容轉(zhuǎn)變?yōu)镮mage
iOS將View的內(nèi)容轉(zhuǎn)變?yōu)镮mage
http://blog.sina.com.cn/s/blog_9c3c519b01014g73.html
15 UITableview最后一行顯示不全
? ? //tableview的高度減去tabbar的高度就好了
????float screenHeight = [[UIScreen mainScreen] applicationFrame].size.height;
??? float scale = [[UIScreen mainScreen] scale];
??? float statusHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
??? float navHeight = self.navigationController.view.frame.size.height;
??? float navBarHeight = self.navigationController.navigationBar.frame.size.height;
??? float tabBarHeight = self.navigationController.tabBarController.tabBar.frame.size.height;
??? float toolBarHeight = self.navigationController.toolbar.frame.size.height;
??? float contentViewHeight = navHeight - statusHeight - navBarHeight -tabBarHeight;
??? LOGDEBUG([NSString stringWithFormat:@"screenHeight:%f, navH:%f, navBarH:%f, tabBarH:%f, toolBarH:%f, contentViewHeight:%f", screenHeight, navHeight, navBarHeight, tabBarHeight, toolBarHeight, contentViewHeight]);
??? // [_flagshipStoreInfoView setFrame:CGRectMake(0, self.mapView.frame.origin.y + self.mapView.frame.size.height, self.view.frame.size.width, contentViewHeight - self.mapView.frame.size.height)];
16 轉(zhuǎn)換NSString為UTF8編碼的函數(shù)
?- (NSString*)URLEncodedString{??
??? NSString *result = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)self, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8);
?? [resultautorelease];
?? returnresult;
}
?- (NSString*)URLDecodedString{
?? NSString*result = (NSString*) CFURLCreateStringByReplacingPercentEscapesUsingEncoding (kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8);
?? [resultautorelease];
?? returnresult;
}
http://blog.csdn.net/zaitianaoxiang/article/details/6651454
17 【NSURL】NSURL各參數(shù)
//測試url
NSURL *url = [NSURL URLWithString:?@"http://www.baidu.com/s?tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709"];
? NSLog(@"Scheme: %@", [url scheme]);
? NSLog(@"Host: %@", [url host]);
? NSLog(@"Port: %@", [url port]);
? NSLog(@"Path: %@", [url path]);
? NSLog(@"Relative path: %@", [url relativePath]);
? NSLog(@"Path components as array: %@", [url pathComponents]);
? NSLog(@"Parameter string: %@", [url parameterString]);
? NSLog(@"Query: %@", [url query]);
? NSLog(@"Fragment: %@", [url fragment]);
? NSLog(@"User: %@", [url user]);
? NSLog(@"Password: %@", [url password]);
顯示如下:
2012-01-10?13:46:01.528 Letter[1758:10d03] Scheme: http
2012-01-10 13:46:01.686?Letter[1758:10d03] Host: www.cocoachina.com
2012-01-10?13:46:01.840 Letter[1758:10d03] Port: (null)
2012-01-10?13:46:01.986 Letter[1758:10d03] Path: /bbs/read.php
2012-01-10?13:46:02.170 Letter[1758:10d03] Relative path: /bbs/read.php
2012-01-10?13:46:02.324 Letter[1758:10d03] Path components as array: (?"/", bbs, "read.php")
2012-01-10?13:46:02.492 Letter[1758:10d03] Parameter string: (null)
2012-01-10?13:46:02.715 Letter[1758:10d03] Query: tid-70265.html
2012-01-10?13:46:02.863 Letter[1758:10d03] Fragment: (null)
2012-01-10?13:46:03.056 Letter[1758:10d03] User: (null)
2012-01-10 13:46:03.427 Letter[1758:10d03]?Password: (null)
http://blog.sina.com.cn/s/blog_45e2b66c01010dm0.html
18 【UI】鍵盤消失的方法
觸摸非輸入?yún)^(qū)(背景)使UITextField(UISearchBar)鍵盤消失的方法
http://blog.sina.com.cn/s/blog_a7c44c8801018c33.html
- (void)resignKeyBoardInView:(UIView *)view
{
??? for (UIView *v inview.subviews) {
??????? if([v.subviews count] > 0) {
??????????? [selfresignKeyBoardInView:v];
??????? }
??????? if ([visKindOfClass:[UITextView class]] || [v isKindOfClass:[UITextField class]]) {
??????????? [v resignFirstResponder];
??????? }
??? }
}
19 【UI】UIActivityIndicatorView的使用(菊花)
iOS UIActivityIndicatorView的使用(菊花)
http://blog.csdn.net/zhaopenghhhhhh/article/details/12092657
UIActivityIndicatorView非常簡單 膛堤,就是一個轉(zhuǎn)圈圈的控件
初始化方法
- initWithActivityIndicatorStyle
控制一個Activity Indicator
- startAnimating
- stopAnimating
- isAnimating
hidesWhenStopped屬性
配置Activity Indicator外觀
activityIndicatorViewStyle屬性
color屬性 ?(iOS 5 ?引入)
常量三個
typedef enum {?
????UIActivityIndicatorViewStyleWhiteLarge,?
????UIActivityIndicatorViewStyleWhite,?
????UIActivityIndicatorViewStyleGray,
} UIActivityIndicatorViewStyle;?
使用方式就是
UIActivityIndicatorView *testActivityIndicator =
[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite]];
testActivityIndicator.center = CGPointMake(100.0f,100.0f);//只能設(shè)置中心手趣,不能設(shè)置大小
[testActivityIndicator setFrame = CGRectMack(100, 100,100, 100)];//不建議這樣設(shè)置,因?yàn)閁IActivityIndicatorView是不能改變大小只能改變位置肥荔,這樣設(shè)置得到的結(jié)果是控件的中心在(100绿渣,100)上,而不是和其他控件的frame一樣左上角在(100燕耿,100)長為100中符,寬為100.
[self addSubview:testActivityIndicator];
testActivityIndicator.color = [UIColor redColor]; //改變?nèi)θΦ念伾珵榧t色;iOS5引入
[testActivityIndicator startAnimating]; //開始旋轉(zhuǎn)
[testActivityIndicator stopAnimating]; //結(jié)束旋轉(zhuǎn)
[testActivityIndicator?setHidesWhenStopped:YES];//當(dāng)旋轉(zhuǎn)結(jié)束時隱藏
????????還有一個是isAnimating方法誉帅,返回一個BOOL值淀散,可以用這個方法來判斷控件是否在旋轉(zhuǎn)
????????initWithActivityIndicatorStyle是UIActivityIndicatorView唯一的初始化方法
????????屬性值是一個枚舉變量,只有三個值:
????UIActivityIndicatorViewStyleWhite;白色圓圈
????UIActivityIndicatorViewStyleWhiteLarge;白色圓圈但是要大些
????UIActivityIndicatorViewStyleGray;灰色圓圈
IOS風(fēng)火輪蚜锨、菊花档插、loading使用
http://my.oschina.net/wangdk/blog/152730
20 【邏輯】NSData,NSImage,NSDictionary,NSString亚再,NSInteger郭膛,F(xiàn)loat,NSURL等等互相轉(zhuǎn)換
NSData轉(zhuǎn)NSDictionary
-(NSMutableDictionary *) getCityInfoDicWithData:(NSData*) cityInfo
{
??? if(cityInfo) {
????????//? NSDictionary * tempDic =[NSJSONSerialization JSONObjectWithData: cityInfo options:? NSJSONReadingMutableContainers error: nil];
??????? NSKeyedUnarchiver*unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:cityInfo];
??????? NSDictionary*tempDic = [unarchiver decodeObjectForKey:_provinceEntity.name];
??????? [unarchiverfinish Decoding];
??????? if(!tempDic) {
??????????? return nil;
??????? }
??????? NSMutableDictionary *dic =[[NSMutableDictionary alloc] initWithDictionary: tempDiccopyItems: YES];
??????? returndic;
??? }
??? else return [[NSMutableDictionary alloc] init];
}
NSDictionary轉(zhuǎn) NSData
-(BOOL)saveEntity
{
??? NSMutableData*data = [[NSMutableData alloc] init];
??? NSKeyedArchiver*archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
??? [archiverencodeObject:self.cityInfoDic forKey:self.provinceEntity.name];
??? [archiverfinish Encoding];
??? self.provinceEntity.cityInfo=data;
??? return YES;
}
NSData针余,NSImage,NSDictionary,NSString饲鄙,NSInteger,F(xiàn)loat圆雁,NSURL等等互相轉(zhuǎn)換
http://blog.163.com/moon_walker/blog/static/213179094201401524753450/
iOS NSDictionary、NSData帆谍、JSON數(shù)據(jù)類型相互轉(zhuǎn)換
http://blog.csdn.net/tangwei019917/article/details/8671535
21 【UI】UIImage如何從URL加載圖像伪朽?
UIImage如何從URL加載圖像?
NSString *myURL = [objectobjectForKey:@"ProductImage"];
cell.imageView.image = [UIImage imageWithData:[NSDatadataWithContentsOfURL:[NSURL URLWithString:myURL]]];