IOS用戶端Coding項目記錄(五),ios用戶端coding項目
1:統一修改導覽列的樣式,在 AppDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; //設定導航條樣式 [self customizeInterface]; if ([Login isLogin]) { [self setupTabViewController]; }else{ [UIApplication sharedApplication].applicationIconBadgeNumber = 0; [self setupLoginViewController]; } [self.window makeKeyAndVisible]; return YES;}- (void)customizeInterface { //設定Nav的背景色和title色 UINavigationBar *navigationBarAppearance = [UINavigationBar appearance]; NSDictionary *textAttributes = nil; if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { [navigationBarAppearance setTintColor:[UIColor whiteColor]];//返回按鈕的箭頭顏色 [[UITextField appearance] setTintColor:[UIColor colorWithHexString:@"0x3bbc79"]];//設定UITextField的游標顏色 [[UITextView appearance] setTintColor:[UIColor colorWithHexString:@"0x3bbc79"]];//設定UITextView的游標顏色 [[UISearchBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0xe5e5e5"]] forBarPosition:0 barMetrics:UIBarMetricsDefault]; textAttributes = @{ NSFontAttributeName: [UIFont boldSystemFontOfSize:kNavTitleFontSize], NSForegroundColorAttributeName: [UIColor whiteColor], }; } else {#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0 [[UISearchBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0xe5e5e5"]]]; textAttributes = @{ UITextAttributeFont: [UIFont boldSystemFontOfSize:kNavTitleFontSize], UITextAttributeTextColor: [UIColor whiteColor], UITextAttributeTextShadowColor: [UIColor clearColor], UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero], };#endif } [navigationBarAppearance setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0x28303b"]] forBarMetrics:UIBarMetricsDefault]; [navigationBarAppearance setTitleTextAttributes:textAttributes];}其中上面的版本判斷:FOUNDATION_EXPORT double NSFoundationVersionNumber;#if TARGET_OS_IPHONE#define NSFoundationVersionNumber_iPhoneOS_2_0 678.24#define NSFoundationVersionNumber_iPhoneOS_2_1 678.26#define NSFoundationVersionNumber_iPhoneOS_2_2 678.29#define NSFoundationVersionNumber_iPhoneOS_3_0 678.47#define NSFoundationVersionNumber_iPhoneOS_3_1 678.51#define NSFoundationVersionNumber_iPhoneOS_3_2 678.60#define NSFoundationVersionNumber_iOS_4_0 751.32#define NSFoundationVersionNumber_iOS_4_1 751.37#define NSFoundationVersionNumber_iOS_4_2 751.49#define NSFoundationVersionNumber_iOS_4_3 751.49#define NSFoundationVersionNumber_iOS_5_0 881.00#define NSFoundationVersionNumber_iOS_5_1 890.10#define NSFoundationVersionNumber_iOS_6_0 992.00#define NSFoundationVersionNumber_iOS_6_1 993.00#define NSFoundationVersionNumber_iOS_7_0 1047.20#define NSFoundationVersionNumber_iOS_7_1 1047.25#endif
2:判斷一張view 是否被載入過用 nil == view.superview
if (nil == view.superview) { //判斷一個view 是否被載入過 如果被載入過,它的superview就不會是nil CGRect frame = scrollView0.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; view.frame = frame; [scrollView0 addSubview:view]; }
3:百度地圖初始化座標範圍
- (void)viewDidLoad { [super viewDidLoad]; [UIApplication sharedApplication].applicationIconBadgeNumber =15; _mapView=[[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)]; BMKCoordinateRegion region; ////表示範圍的結構體 region.center.latitude = 24.27;// 中心中 region.center.longitude = 118.06; region.span.latitudeDelta = 0.1;//經度範圍(設定為0.1表示顯示範圍為0.2的緯度範圍) region.span.longitudeDelta = 0.1;//緯度範圍 [_mapView setRegion:region]; [self.baiduView addSubview:_mapView];}自訂圖釘的圖片:- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation{ if ([annotation isKindOfClass:[BMKPointAnnotation class]]) { BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"]; newAnnotationView.pinColor = BMKPinAnnotationColorPurple; newAnnotationView.animatesDrop = YES;// 設定該標註點動畫顯示 newAnnotationView.image = [UIImage imageNamed:@"iphone"]; //把圖釘換成別的圖片 return newAnnotationView; } return nil;}
4:隱藏鍵盤
當前視圖上有多個uitextfield時,來隱藏鍵盤, 先遍曆視圖的所有子視圖來 如果是UITextField就將其設為非第一響應 當然,如果要隱藏子視圖上的UITextField的話可以進一步判斷view的subviews的個數,如果大於1則遍曆view的子視圖,然後作類似操作//隱藏鍵盤 當前視圖上有多個uitextfieldfor(UIView *view in [self.view subviews]){ if(view is kindofclass:[UITextField Class]) { [view resignfirstrespond]; }}直接用 [self.view endEditing:NO]直接取消當前Window上的各種view的鍵盤 [[[UIApplication sharedApplication] keyWindow] endEditing:YES];或者使用如下代碼- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(done:)];tapGestureRecognizer.numberOfTapsRequired = 1;[self.view addGestureRecognizer: tapGestureRecognizer]; //只需要點擊非文字輸入地區就會響應hideKeyBoardreturn YES;}
5:UIView中的座標轉換
// 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;// 將像素point從view中轉換到當前視圖中,返回在當前視圖中的像素值- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;// 將rect由rect所在視圖轉換到目標視圖view中,返回在目標視圖view中的rect- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;// 將rect從view中轉換到當前視圖中,返回在當前視圖中的rect- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;例把UITableViewCell中的subview(btn)的frame轉換到 controllerA中// controllerA 中有一個UITableView, UITableView裡有多行UITableVieCell,cell上放有一個button// 在controllerA中實現:CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];或CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];// 此rc為btn在controllerA中的rect或當已知btn時:CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];或CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];比如:CGPoint origin = [self convertPoint:CGPointZero toView:[UIApplication sharedApplication].keyWindow]; 把self的0點座標系,放到keyWindow的座標系換算一下,獲得一個“絕對的”座標一個在父控制項中的座標為0,0 其實父控制項本來有座標200,200 通過上面可以獲得這個200,200值
6:彈出一個視圖,並有一個背影的視圖(大體代碼)
- (UIView *)myTapBackgroundView{ if (!_myTapBackgroundView) { _myTapBackgroundView = ({ UIView *view = [[UIView alloc] initWithFrame:kScreen_Bounds]; view.backgroundColor = [UIColor clearColor]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeShowing)]; [view addGestureRecognizer:tap]; view; }); } return _myTapBackgroundView;}- (UIView *)myContentView{ if (!_myContentView) { _myContentView = ({ UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; view.backgroundColor = [UIColor whiteColor]; view; }); } return _myContentView;}- (void)changeShowing{ [kKeyWindow endEditing:YES]; if (!_myContentView) {//未載入過 [self loadUIElement]; } CGPoint origin = [self convertPoint:CGPointZero toView:kKeyWindow]; CGFloat contentHeight = self.isShowing? 0: kCodeBranchTagButton_ContentHeight; if (self.isShowing) {//隱藏 self.enabled = NO; [UIView animateWithDuration:0.3 animations:^{ self.myTapBackgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0]; self.myContentView.alpha = 0; self.myContentView.frame = CGRectMake(0, origin.y-contentHeight, kScreen_Width, contentHeight); self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, DEGREES_TO_RADIANS(180)); } completion:^(BOOL finished) { [self.myTapBackgroundView removeFromSuperview]; [self.myContentView removeFromSuperview]; self.enabled = YES; self.isShowing = NO; }]; }else{//顯示 self.myContentView.frame = CGRectMake(0, origin.y, kScreen_Width, 0); [kKeyWindow addSubview:self.myTapBackgroundView]; [kKeyWindow addSubview:self.myContentView]; self.enabled = NO; [UIView animateWithDuration:0.3 animations:^{ self.myTapBackgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2]; self.myContentView.alpha = 1.0; self.myContentView.frame = CGRectMake(0, origin.y-contentHeight, kScreen_Width, contentHeight); self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, DEGREES_TO_RADIANS(180)); } completion:^(BOOL finished) { self.enabled = YES; self.isShowing = YES; }]; }}其中:#define kScreen_Bounds [UIScreen mainScreen].bounds#define kKeyWindow [UIApplication sharedApplication].keyWindow