iOS中 項目開發易錯知識點總結

點擊return取消textView 的響應者

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. - (BOOL)textFieldShouldReturn:(UITextField *)textField  
  2. {  
  3.     [_contactTextFiled resignFirstResponder];  
  4.     return YES;  
  5. }  
  6.   
  7. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{  
  8.      
  9.     if([text isEqualToString:@"\n"]){  
  10.          
  11.         [textView resignFirstResponder];  
  12.         [_contactTextFiled becomeFirstResponder];  
  13.         return YES;  
  14.     }  
  15.     return YES;  
  16. }  

 

iOS一行代碼將全部子視圖從父視圖上移除

這裏主要利用了一個makeObjectsPerformSelector:函數。這個函數能夠在不少場景下使用從而簡化代碼。

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. [xxxView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];  

 

有效解決刷新單個cell或者section閃一下的問題:

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. [UIView setAnimationsEnabled:NO];  
  2. [_listTable beginUpdates];  
  3. [_listTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];  
  4. [_listTable endUpdates];  
  5. [UIView setAnimationsEnabled:YES];  

每日更新關注:http://weibo.com/hanjunqiang  新浪微博!xcode

 

畫出下列曲線:

 

 

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. UIView *myCustomView = [[UIView alloc]initWithFrame:CGRectMake(0, 204,kScreenWidth, 120)];  
  2. myCustomView.backgroundColor = [UIColor whiteColor];  
  3. [view addSubview:myCustomView];  
  4.   
  5. UIBezierPath *bezierPath = [UIBezierPath bezierPath];  
  6. [bezierPath moveToPoint:CGPointMake(0,0)];  
  7. [bezierPath addCurveToPoint:CGPointMake(myCustomView.width, 0) controlPoint1:CGPointMake(0, 0) controlPoint2:CGPointMake(myCustomView.width/2, 40)];  
  8. [bezierPath addLineToPoint:CGPointMake(myCustomView.width, myCustomView.height)];  
  9. [bezierPath addLineToPoint:CGPointMake(0, myCustomView.height)];  
  10. [bezierPath closePath];  
  11.   
  12. CAShapeLayer *shapLayer = [CAShapeLayer layer];  
  13. shapLayer.path = bezierPath.CGPath;  
  14. myCustomView.layer.mask = shapLayer;  
  15. myCustomView.layer.masksToBounds = YES;  

 

 

當你使用 UISearchController 在 UITableView 中實現搜索條,在搜索框已經激活並推入新的 VC 的時候會發生搜索框重疊的狀況:那就是 definesPresentationContext 這個布爾值!

每日更新關注:http://weibo.com/hanjunqiang  新浪微博!app

TabBar的隱藏與消失:

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. - (void)hidesTabBar:(BOOL)hidden{  
  2.     [UIView beginAnimations:nil context:NULL];  
  3.     [UIView setAnimationDuration:0];  
  4.      
  5.     for (UIView *view in self.tabBarController.view.subviews){  
  6.          
  7.         if ([view isKindOfClass:[UITabBar class]]) {  
  8.             if (hidden) {  
  9.                 [view setFrame:CGRectMake(view.frame.origin.x, [UIScreen mainScreen].bounds.size.height, view.frame.size.width , view.frame.size.height)];  
  10.             }else{  
  11.                 [view setFrame:CGRectMake(view.frame.origin.x, [UIScreen mainScreen].bounds.size.height - 49, view.frame.size.width, view.frame.size.height)];  
  12.             }  
  13.         }else{  
  14.             if([view isKindOfClass:NSClassFromString(@"UITransitionView")]){  
  15.                 if (hidden){  
  16.                     [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, [UIScreen mainScreen].bounds.size.height)];  
  17.                 }else{  
  18.                      
  19.                     [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 49 )];  
  20.                 }  
  21.             }  
  22.         }  
  23.     }  
  24.     [UIView commitAnimations];  
  25. }  

 

 

獲取cell上按鈕所在分區和行數:

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. UIView *view = [sender superview]; // 獲取父視圖的view  
  2. GCCollectGroupCellTableViewCell *cell = (GCCollectGroupCellTableViewCell*)[view superview]; // 獲取cell  
  3. NSIndexPath *indexPath = [_listTable indexPathForCell:cell]; // 獲取cell對應的分區  
  4. NSLog(@"%@",detailArr[indexPath.section-1][indexPath.row][@"comname"]);  

 

 

NSAttributedString 與NSString互轉:

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[_CompanyFileds.comname dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];  
  2. _CompanyFileds.comname = attrStr.string;  

 

 

監控UITextField 變化:

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. // 註冊監聽  
  2.     [[NSNotificationCenter defaultCenter]postNotificationName:UITextFieldTextDidChangeNotification object:nil];  
  3.     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeForKeyWord:) name:UITextFieldTextDidChangeNotification object:nil];  
  4.   
  5.   
  6. // 監聽_pageTextField.text變化  
  7. - (void)changeForKeyWord:(NSNotification *)sender  
  8. {  
  9.     [self checkNum:_pageTextField.text];  
  10. }  
  11. - (BOOL)checkNum:(NSString *)str  
  12. {  
  13.     NSString *regex = @"^[0-9]+(.[0-9]{2})?$";  
  14.     NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];  
  15.     BOOL isMatch = [pred evaluateWithObject:str];  
  16.     if (!isMatch && _pageTextField.text.length>0) {  
  17.         UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"頁碼只能輸入數字哦" delegate:self cancelButtonTitle:@"從新輸入" otherButtonTitles:nil, nil nil];  
  18.         alertView.delegate = self;  
  19.         [alertView show];  
  20.         return NO;  
  21.     }  
  22.     return YES;  
  23. }  

 

監聽UITextField的點擊事件

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. [[NSNotificationCenter defaultCenter]postNotificationName:UITextFieldTextDidBeginEditingNotification object:nil];   
  2.         [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterEdited:) name:UITextFieldTextDidBeginEditingNotification object:nil];   
  3.    
  4. - (void)enterEdited:(NSNotification *)sender   
  5. {   
  6.     事件寫這裏!但願幫到你!   
  7. }   


每日更新關注:http://weibo.com/hanjunqiang  新浪微博!ide

 

解決添加到父視圖手勢影響子視圖手勢的解決辦法:(手勢衝突)

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. //方法一:  
  2. #pragma mark--UIGestureRecognizerDelegate  
  3. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch  
  4. {  
  5.     if([touch.view isKindOfClass:[UIButton class]]||[touch.view isKindOfClass:[UITableViewCell class]]||[touch.view isKindOfClass:[UITextField class]]||[touch.view isKindOfClass:[UITextView class]])  
  6.     {  
  7.         return NO;  
  8.     }  
  9.     return YES;  
  10. }  
[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. //方法二:  
  2. //UIView的exclusiveTouch屬性  
  3. //經過設置[self setExclusiveTouch:YES];能夠達到同一界面上多個控件接受事件時的排他性,從而避免一些問題。  

 

UITextField 邊框樣式及內容調整:

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. //1. 設置的時候在ib裏面記得選擇無邊框的,要否則隨便你設置,都是無效的,也是坑死了。  
  2.   _textBoxName.layer.borderWidth=1.0f;  
  3.     _textBoxName.layer.borderColor=[UIColorcolorWithRed:0xbf/255.0fgreen:0xbf/255.0fblue:0xbf/255.0falpha:1].CGColor;  
  4.   
  5.    //2.在uitextfield 中文字最左邊距離左側邊框的距離  
  6.     _textBoxName.leftView=[[UIViewalloc] initWithFrame:CGRectMake(0,0, 16,51)];  
  7.     _textBoxName.leftViewMode=UITextFieldViewModeAlways;  

 

 

圖片旋轉控制:

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. #pragma mark ----- 更新按鈕動畫  
  2. - (void)rotate360DegreeWithImageViews:(UIImageView *)myViews{  
  3.     CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];  
  4.     rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];  
  5.     rotationAnimation.duration = 1.0;  
  6.     rotationAnimation.cumulative = YES;rotate360DegreeWithImageViews  
  7.     rotationAnimation.repeatCount = 100000;  
  8.     [myViews.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];  
  9. }  
  10. [myViews.layer removeAllAnimations]; // 中止  

 

 

改變cell的選中顏色:

 

[plain]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
  1. cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];  
  2. cell.selectedBackgroundView.backgroundColor = COLOR_BACKGROUNDVIEW;  
  3. 不須要任何顏色能夠這麼設置:  
  4. cell.selectionStyle = UITableViewCellSelectionStyleNone;  



 

取整問題:

 

[plain]  view plain  copy
 在CODE上查看代碼片派生到個人代碼片
    1. Objective-C拓展了C,天然不少用法是和C一致的。好比浮點數轉化成整數,就有如下四種狀況。  
    2. 1.簡單粗暴,直接轉化  
    3.   
    4.   
    5. float f = 1.5; int a; a = (int)f; NSLog("a = %d",a);  
    6. 輸出結果是1。(int)是強制類型轉化,丟棄浮點數的小數部分。  
    7.   
    8. 2.高斯函數,向下取整  
    9.   
    10.   
    11. float f = 1.6; int a; a = floor(f); NSLog("a = %d",a);  
    12. 輸出結果是1。floor()方法是向下取整,相似於數學中的高斯函數 [].取得不大於浮點數的最大整數,對於正數來講是捨棄浮點數部分,對於複數來講,捨棄浮點數部分後再減1.  
    13.   
    14. 3.ceil函數,向上取整。  
    15.   
    16.   
    17. float f = 1.5; int a; a = ceil(f); NSLog("a = %d",a);  
    18. 輸出結果是2。ceil()方法是向上取整,取得不小於浮點數的最小整數,對於正數來講是捨棄浮點數部分並加1,對於複數來講就是捨棄浮點數部分.  
    19.   
    20. 4.經過強制類型轉換四捨五入。  
    21.   
    22. float f = 1.5; int a; a = (int)(f+0.5); NSLog("a = %d",a);  
相關文章
相關標籤/搜索