? 前言:? ? ?
?一到公司報道那時,便著手獨立的去完成了一個項目勉痴,其中的辛酸淚也是不足為外人道也赫模。這次算是一個新型的app,仍然是獨立開發(fā)蚀腿,但心境和想法卻是完全的不同嘴瓤。下面說一次以前做開發(fā)時常常忽略的知識,也算是一種復(fù)習(xí)了吧莉钙。下面言歸正傳:
1廓脆、自帶鍵盤的next用法
這算是比較常用的了吧,只要我們想到登錄和注冊界面磁玉,就應(yīng)該自然而然的想到 - 鍵盤的next用法(當(dāng)然不排除相當(dāng)多的軟件并沒有實現(xiàn)這個功能)停忿。
想法也就是:用戶在輸入用戶名后,點擊next跳轉(zhuǎn)到下一個輸入框蚊伞。其實也不用想的如此復(fù)雜席赂,在登錄界面,我們完全可以用一個 if 語句做判斷时迫,去實現(xiàn)鍵盤的響應(yīng)和注銷響應(yīng)颅停。
代碼如下:?
?? - (BOOL)textFieldShouldReturn:(UITextField *)textField? ? {?
?? ? UITextField *passTextFiled = (UITextField *)[self.view viewWithTag:201];? ?
?? if (textField.tag == 200) {? ??
? ? ? [passTextFiled becomeFirstResponder];? ?
?? ? }else{? ? ?
? ? ? [passTextFiled resignFirstResponder];??
? ? }? ?
?? return YES;?
?? }
這樣就簡單的在變成響應(yīng)和注銷響應(yīng)之間實現(xiàn)了輸入框的切換。
但還是存在一個問題掠拳,如果我實現(xiàn)注冊界面的跳轉(zhuǎn)輸入框癞揉,并且在輸入框很多的情況下,顯然如果我仍是這樣判斷,會顯得啰嗦和冗余喊熟。
我曾想過使用計數(shù)的方式柏肪,記錄每一次的點擊next后的tag,但實踐中失敗了芥牌,因為當(dāng)我不通過next而是直接用手去觸摸選擇輸入框時烦味,則無法計數(shù)。
?這里先留個空白壁拉,等把手里的項目完成時再研究這個問題
2谬俄、UITextField的糾錯和首字母大小寫問題
這個問題只是簡單的屬性問題,在開發(fā)時如果忘記扇商,建議多點進去看看凤瘦,(如:command + UITextField)? ?
?//輸入框中是否有個叉號,在什么時候顯示案铺,用于一次性刪除輸入框中的內(nèi)容? ? self.clearButtonMode = UITextFieldViewModeWhileEditing;? ??
//是否糾錯,本人感覺糾錯是一個很反感的東西蔬芥,但UITextField默認(rèn)是 YES,這里我改成了NO? ? ? self.autocorrectionType = UITextAutocorrectionTypeNo;? ? ? ?
?//再次編輯就清空? ? ? self.clearsOnBeginEditing = YES;? ? ? ?
?//設(shè)置自動縮小顯示的最小字體大小? ? ? self.minimumFontSize = 15;? ? ? ??
//設(shè)置鍵盤的樣式,本人感覺這個設(shè)置對用戶體驗有影響控汉。比如說我想打電話笔诵,Type為UIKeyboardTypeNumberPad,而不是其他 ? ??
?self.keyboardType = UIKeyboardTypeNamePhonePad;
3、iOS中圖片的幾種拉伸方法的使用
以下是apple給出的3種方法: ? ??
?? UIImage *image = [UIImage imageNamed:@"picture"];? ? ? ??
?//iOS5之前? ? ?
?// 左端蓋寬度? ? ? ??
NSInteger leftCapWidth = image.size.width * 0.5f;? ? ? ?
?NSInteger topCapHeight = image.size.height * 0.8f;? ? ? ??
image = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];? ? ? ??
//iOS5之后? ? ? ?
?UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height * 0.8f,image.size.width * 0.5f, 40, 40);? ? ? ?
?image = [image resizableImageWithCapInsets:insets];? ? ??
?//iOS6之后? ? ? ? UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height * 0.8f,image.size.width * 0.5f, 40, 40);? ? ? ?
?image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];? ??
下面是使用的解釋: ??
/**? ?
?*? - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;? ??
?*??
? *? leftCapWidth代表左端蓋寬度姑子,topCapHeight代表頂端蓋高度乎婿。系統(tǒng)會自動計算出右端蓋寬度(rightCapWidth)和底端蓋高度(bottomCapHeight)? ??
?* 算法如下:? ??
?* width為圖片寬度? rightCapWidth = width - leftCapWidth - 1;? ? ? ?
?* height為圖片高度 bottomCapHeight = height - topCapHeight - 1? ??
?*? ??
?*經(jīng)過計算,你會發(fā)現(xiàn)中間的可拉伸區(qū)域只有1x1? ??
?* ??
* stretchWidth為中間可拉伸區(qū)域的寬度? ? ? ? stretchWidth = width - leftCapWidth - rightCapWidth = 1;? ? ? ?
?* stretchHeight為中間可拉伸區(qū)域的高度? ? ? ? stretchHeight = height - topCapHeight - bottomCapHeight = 1;? ??
?*? ??
?*因此街佑,使用這個方法只會拉伸圖片中間1x1的區(qū)域谢翎,并不會影響到邊緣和角落。? ??
?*/
/**? ?
?* - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets? ??
*? 這個方法只接收一個UIEdgeInsets類型的參數(shù)沐旨,可以通過設(shè)置UIEdgeInsets的left森逮、right、top磁携、bottom來分別指定左端蓋寬度褒侧、右端蓋寬度、頂端蓋高度谊迄、底端蓋高度? ??
?*?*/ ? ? ??
?/**? ??
?*? - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode? ??
對比iOS5.0中的方法闷供,只多了一個UIImageResizingMode參數(shù),用來指定拉伸的模式: ? ? ? UIImageResizingModeStretch:拉伸模式统诺,通過拉伸UIEdgeInsets指定的矩形區(qū)域來填充圖片? ? UIImageResizingModeTile:平鋪模式歪脏,通過重復(fù)顯示UIEdgeInsets指定的矩形區(qū)域來填充圖片? ??
?**/
圖片拉伸的以上內(nèi)容參考文章 《iOS圖片拉伸技巧》講的很好,建議大家多看看粮呢。
4唾糯、iOS中喚起自帶瀏覽器(safari)的方法
也是一個簡單的一句代碼 怠硼,這種也就是知道了就知道了,不知道就是抓耳撓腮吧?
?? NSURL *url = [NSURL URLWithString:urlStr];? ? [[UIApplication sharedApplication] openURL:url];
5移怯、iOS中喚起電話界面
app內(nèi)部喚起電話界面的實現(xiàn),是找了很多資料才有的一個結(jié)論(安全这难,并且撥打完之后可以返回app)
實現(xiàn)的方法是使用UIWebView 加載電話舟误,目前這種方法是合法的柄驻,App Store也允許通過的盆繁。??
?NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",newPhoneString]]; ? ??
?if (_phoneCallWebView) {?
?? ? ? ? ? ? ? [_phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];??
?}
但如果使用下面的這種方法梁肿,則可能不允許通過審核? ? [
[UIApplication sharedApplication] openURL:phoneURL];
6富岳、iOS 調(diào)用地圖的方法
app內(nèi)部調(diào)用第三方app练慕,這里有一個詳細(xì)的文章《 IOS實現(xiàn)應(yīng)用內(nèi)打開第三方地圖app進行導(dǎo)航》
- app內(nèi)部調(diào)用地圖脏毯,需要先檢測用戶手機上是否已經(jīng)安裝地圖app滴某。
我們常用的地圖app有:高德乡翅、百度剪个;國外有:谷歌(Google Map )秧骑。當(dāng)然還有蘋果自帶的地圖,隨著iOS10的發(fā)布扣囊,蘋果地圖這塊也有很大的完善和進步乎折。
我使用Xcode模擬器實現(xiàn)時,會提示:
?1侵歇、-canOpenURL: failed for URL: "iosamap://" - error: "(null)" 骂澄。原因是:模擬器上沒有高德地圖。?
2惕虑、-canOpenURL: failed for URL: "comgooglemaps://" - error: "This app is not allowed to query for scheme comgooglemaps"坟冲。 原因是:LSApplicationQueriesSchemes 我自己馬虎設(shè)成了字典類型。
首先說好的是iOS9以后溃蔫,我們在app內(nèi)部要跳轉(zhuǎn)到其他軟件時健提,需要在 Info.plist 中添加白名單酒唉。
方法為:在Info.plist中添加 key :LSApplicationQueriesSchemes? 類型為:Array矩桂。
將高德、百度痪伦、谷歌的值填進去分別是:**iosamap**|**baidumap**|**comgooglemaps**
圖片:
?至于下面代碼中的URLScheme 和appName??
是在Info.plist 的URL types中添加? URL Scheme( URL Schemes 是一個數(shù)組,允許應(yīng)用定義多個 URL schemes网沾。 )? 和? URL identifier(建議采用反轉(zhuǎn)域名的方法保證該名字的唯一性攒射,比如 com.yourApp.www)
圖片如下:
?下面是具體的代碼:
?__block NSString *urlScheme = urlScheme;? ?
?__block NSString *appName = appName;? ??
__block CLLocationCoordinate2D coordinate = self.coordinate;? ? ? ??
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"選擇地圖" message:nil preferredStyle:UIAlertControllerStyleActionSheet];? ? ? ? ??
//apple自帶的地圖不需要判斷? ? ? ??
UIAlertAction *action = [UIAlertAction actionWithTitle:@"蘋果地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {? ? ??
? ? ? ? ? ?MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];?
?? ? ? ? ? MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];? ? ? ? ? ? ? ? ? ? ??
?[MKMapItem openMapsWithItems:@[currentLocation, toLocation]? ? ? ? ? ? ? ? ? ? ? ? ? launchOptions:@{
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];? ? ? ??
}];? ? ? ??
?[alert addAction:action];? ? ? ??
?//判斷百度地圖? ?
?if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])? ? ??
{? ? ? ? UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {? ? ? ? ? ? ? ? ? ? ? ?
?NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];? ? ? ? ?
?}];? ? ? ??
[alert addAction:action];? ? ??
}? ? ?
?//判斷高德地圖? ? ?
?if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])? ? ??
?{? ? ? ??
UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {? ? ? ? ? ? ? ? ? ? ? ??
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];??
? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];? ? ? ? ? ? ? ? ? ? }];? ? ? ?
?[alert addAction:action];? ??
}? ? ? ??
?//判斷谷歌地圖? ?
?if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])? ? {? ? ? ?
?UIAlertAction *action = [UIAlertAction actionWithTitle:@"谷歌地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {? ? ? ? ? ? ? ? ? ? ? ?
?NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];? ? ? ? ? ? ? ? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];? ? ? ? ??
}];? ? ? ? ? ? ? ??
[alert addAction:action];? ??
}? ? ? ??
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];? ? [alert addAction:action];? ? ? ??
[self presentViewController:alert animated:YES completion:^{? ? ? ? ??
}];? ??
}