? 前言:? ? ?
?一到公司報道那時,便著手獨立的去完成了一個項目,其中的辛酸淚也是不足為外人道也。這次算是一個新型的app,仍然是獨立開發,但心境和想法卻是完全的不同。下面說一次以前做開發時常常忽略的知識,也算是一種復習了吧。下面言歸正傳:
1、自帶鍵盤的next用法
這算是比較常用的了吧,只要我們想到登錄和注冊界面,就應該自然而然的想到 - 鍵盤的next用法(當然不排除相當多的軟件并沒有實現這個功能)。
想法也就是:用戶在輸入用戶名后,點擊next跳轉到下一個輸入框。其實也不用想的如此復雜,在登錄界面,我們完全可以用一個 if 語句做判斷,去實現鍵盤的響應和注銷響應。
代碼如下:?
?? - (BOOL)textFieldShouldReturn:(UITextField *)textField? ? {?
?? ? UITextField *passTextFiled = (UITextField *)[self.view viewWithTag:201];? ?
?? if (textField.tag == 200) {? ??
? ? ? [passTextFiled becomeFirstResponder];? ?
?? ? }else{? ? ?
? ? ? [passTextFiled resignFirstResponder];??
? ? }? ?
?? return YES;?
?? }
這樣就簡單的在變成響應和注銷響應之間實現了輸入框的切換。
但還是存在一個問題,如果我實現注冊界面的跳轉輸入框,并且在輸入框很多的情況下,顯然如果我仍是這樣判斷,會顯得啰嗦和冗余。
我曾想過使用計數的方式,記錄每一次的點擊next后的tag,但實踐中失敗了,因為當我不通過next而是直接用手去觸摸選擇輸入框時,則無法計數。
?這里先留個空白,等把手里的項目完成時再研究這個問題
2、UITextField的糾錯和首字母大小寫問題
這個問題只是簡單的屬性問題,在開發時如果忘記,建議多點進去看看,(如:command + UITextField)? ?
?//輸入框中是否有個叉號,在什么時候顯示,用于一次性刪除輸入框中的內容? ? self.clearButtonMode = UITextFieldViewModeWhileEditing;? ??
//是否糾錯,本人感覺糾錯是一個很反感的東西,但UITextField默認是 YES,這里我改成了NO? ? ? self.autocorrectionType = UITextAutocorrectionTypeNo;? ? ? ?
?//再次編輯就清空? ? ? self.clearsOnBeginEditing = YES;? ? ? ?
?//設置自動縮小顯示的最小字體大小? ? ? self.minimumFontSize = 15;? ? ? ??
//設置鍵盤的樣式,本人感覺這個設置對用戶體驗有影響。比如說我想打電話,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代表頂端蓋高度。系統會自動計算出右端蓋寬度(rightCapWidth)和底端蓋高度(bottomCapHeight)? ??
?* 算法如下:? ??
?* width為圖片寬度? rightCapWidth = width - leftCapWidth - 1;? ? ? ?
?* height為圖片高度 bottomCapHeight = height - topCapHeight - 1? ??
?*? ??
?*經過計算,你會發現中間的可拉伸區域只有1x1? ??
?* ??
* stretchWidth為中間可拉伸區域的寬度? ? ? ? stretchWidth = width - leftCapWidth - rightCapWidth = 1;? ? ? ?
?* stretchHeight為中間可拉伸區域的高度? ? ? ? stretchHeight = height - topCapHeight - bottomCapHeight = 1;? ??
?*? ??
?*因此,使用這個方法只會拉伸圖片中間1x1的區域,并不會影響到邊緣和角落。? ??
?*/
/**? ?
?* - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets? ??
*? 這個方法只接收一個UIEdgeInsets類型的參數,可以通過設置UIEdgeInsets的left、right、top、bottom來分別指定左端蓋寬度、右端蓋寬度、頂端蓋高度、底端蓋高度? ??
?*?*/ ? ? ??
?/**? ??
?*? - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode? ??
對比iOS5.0中的方法,只多了一個UIImageResizingMode參數,用來指定拉伸的模式: ? ? ? UIImageResizingModeStretch:拉伸模式,通過拉伸UIEdgeInsets指定的矩形區域來填充圖片? ? UIImageResizingModeTile:平鋪模式,通過重復顯示UIEdgeInsets指定的矩形區域來填充圖片? ??
?**/
圖片拉伸的以上內容參考文章 《iOS圖片拉伸技巧》講的很好,建議大家多看看。
4、iOS中喚起自帶瀏覽器(safari)的方法
也是一個簡單的一句代碼 ,這種也就是知道了就知道了,不知道就是抓耳撓腮吧?
?? NSURL *url = [NSURL URLWithString:urlStr];? ? [[UIApplication sharedApplication] openURL:url];
5、iOS中喚起電話界面
app內部喚起電話界面的實現,是找了很多資料才有的一個結論(安全,并且撥打完之后可以返回app)
實現的方法是使用UIWebView 加載電話,目前這種方法是合法的,App Store也允許通過的。??
?NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",newPhoneString]]; ? ??
?if (_phoneCallWebView) {?
?? ? ? ? ? ? ? [_phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];??
?}
但如果使用下面的這種方法,則可能不允許通過審核? ? [
[UIApplication sharedApplication] openURL:phoneURL];
6、iOS 調用地圖的方法
app內部調用第三方app,這里有一個詳細的文章《 IOS實現應用內打開第三方地圖app進行導航》
- app內部調用地圖,需要先檢測用戶手機上是否已經安裝地圖app。
我們常用的地圖app有:高德、百度;國外有:谷歌(Google Map )。當然還有蘋果自帶的地圖,隨著iOS10的發布,蘋果地圖這塊也有很大的完善和進步。
我使用Xcode模擬器實現時,會提示:
?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 我自己馬虎設成了字典類型。
首先說好的是iOS9以后,我們在app內部要跳轉到其他軟件時,需要在 Info.plist 中添加白名單。
方法為:在Info.plist中添加 key :LSApplicationQueriesSchemes? 類型為:Array。
將高德、百度、谷歌的值填進去分別是:**iosamap**|**baidumap**|**comgooglemaps**
圖片:
?至于下面代碼中的URLScheme 和appName??
是在Info.plist 的URL types中添加? URL Scheme( URL Schemes 是一個數組,允許應用定義多個 URL schemes。 )? 和? URL identifier(建議采用反轉域名的方法保證該名字的唯一性,比如 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:^{? ? ? ? ??
}];? ??
}