zoukankan      html  css  js  c++  java
  • 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:@" "]){  
    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  新浪微博!

    画出下列曲线:

    [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  新浪微博!

    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  新浪微博!

    解决添加到父视图手势影响子视图手势的解决办法:(手势冲突)

    [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);  
  • 相关阅读:
    sql语句执行顺序
    ThinkPHP的入门学习目录结构及基础知识
    IE6的PNG透明解决方案
    用CSS画三角形
    position:sticky介绍 页面滚动导航条始终在最顶部的实现方法
    那些年我们一起清除过的浮动
    "自适应网页设计"到底是怎么做到的?其实并不难。
    jQuery formValidator表单验证插件(详解)
    学习10分钟,改变你的程序员生涯【转载】
    最差的时光 枯木
  • 原文地址:https://www.cnblogs.com/tangyuanby2/p/5805683.html
Copyright © 2011-2022 走看看