美文网首页
日常更新知识点

日常更新知识点

作者: 子疯zp | 来源:发表于2018-08-15 11:00 被阅读75次

    1、添加系统提示音和震动
    2、在xib 中使用 UIScrollView,但是问题是.m文件中设置contentsize 无效
    3、将数组中的内容分拼接成字符串
    4、判断字典是否包含特定Key值
    5、过滤emoji表情键盘输入,包含输入哈哈引出的微笑表情
    6、dismissViewController到根视图
    7、pop返回到指定控制器
    8、数据处理 (比较两个数组,并除去相同元素)

    1、添加系统提示音和震动

    • 工程下添加AudioToolbox.framework
    • 添加头文件#import <AudioToolbox/AudioToolbox.h>
    • appDelegate的代理方法didReceiveRemoteNotification中添加
      AudioServicesPlaySystemSound(1007);//系统提示音 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动

    2、在xib 中使用 UIScrollView,但是问题是.m文件中设置contentsize 无效

    • 问题分析
      xib 中勾选了autolayout选项,在autolayout下,iOS计算UIScrollView的contentsize的机制是略有不同。在autolayout中,会在viewDidAppear之前根据subview的constraint重新计算UIScrollView的contentsize。 当在viewdidload里手动设置contentsize时,会再重新计算一次,把前面手动设置的值覆盖掉。
    • 解决方法
      1 去除autolayout选项,自己手动设置contentsize
      2 如果要使用autolayout,则需要设置subview的constraint,让系统自动根据constraint计算出contentsize
      3 在viewDidAppear里面自己手动设置contentsize

    3、将数组中的内容分拼接成字符串

    NSString *string = [array componentsJoinedByString:@","];//用,分割
    
    打印array:(
        1,
        2,
    )
    打印string:1,2
    

    4、判断字典是否包含特定Key值

    if([[dict allKeys] containsObject:@"key"]){
       //有
    }else{
       //没有
    }
    

    5、过滤emoji表情键盘输入,包含输入哈哈引出的微笑表情

    当选择表情键盘输入时return NO

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
       // 不让输入表情
       if ([textView isFirstResponder]) {
           //表情键盘
           if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
               return NO;
           }else{//
               return ![self stringContainsEmoji:text];
           }
           
       }
       
       return YES;
    }
    // 过滤所有表情  https://gist.github.com/cihancimen/4146056
    - (BOOL)stringContainsEmoji:(NSString *)string {
       __block BOOL returnValue = NO;
       [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
        ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
            
            const unichar hs = [substring characterAtIndex:0];
            // surrogate pair
            if (0xd800 <= hs && hs <= 0xdbff) {
                if (substring.length > 1) {
                    const unichar ls = [substring characterAtIndex:1];
                    const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                    if (0x1d000 <= uc && uc <= 0x1f77f) {
                        returnValue = YES;
                    }
                }
            } else if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                if (ls == 0x20e3) {
                    returnValue = YES;
                }
                
            } else {
                // non surrogate
    //             if (0x2100 <= hs && hs <= 0x27ff) {
    //                 returnValue = YES;
    //             }
                if (0x2B05 <= hs && hs <= 0x2b07) {
                    returnValue = YES;
                } else if (0x2934 <= hs && hs <= 0x2935) {
                    returnValue = YES;
                } else if (0x3297 <= hs && hs <= 0x3299) {
                    returnValue = YES;
                } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                    returnValue = YES;
                }
            }
        }];
       
       return returnValue;
    }
    
    

    6、dismissViewController到根视图

    如果是在Controller中 则代码如下

     UIViewController *vc =  self;
     while (vc.presentingViewController) {
         vc = vc.presentingViewController;
     }
     [vc dismissViewControllerAnimated:YES completion:nil];  
    

    如果是在视图(View)中 则代码如下

    -(void)backClick{  
         UIViewController *vc =  [self viewController];  
         while (vc.presentingViewController) {  
              vc = vc.presentingViewController;  
         }  
         [vc dismissViewControllerAnimated:YES completion:nil];  
     }  
    
     /** 获取当前view的controller */  
    - (UIViewController *)viewController  
    {  
        for (UIView *next = [self superview]; next; next = next.superview) {  
             UIResponder *nextResponser = [next nextResponder];  
    
             if ([nextResponser isKindOfClass:[UIViewController class]]) {  
                  return (UIViewController *)nextResponser;  
             }  
        }  
       return nil;  
    }  
    

    该代码可返回到根(通过present过来的)Controller

    7、pop返回到指定控制器

    方法一

     UIViewController *viewCtl = self.navigationController.viewControllers[1];
    
    [self.navigationController popToViewController:viewCtl animated:YES];
    

    方法二

    for (UIViewController *controller in self.navigationController.viewControllers) {
              if ([controller isKindOfClass:[SetupViewController class]]) {
                        SetupViewController *revise =(SetupViewController *)controller;
                        [self.navigationController popToViewController:revise animated:YES];
              }
    }
    

    8、数据处理 (比较两个数组,并除去相同元素)

        NSArray * arr1 = @[@1,@2,@3];
        NSArray * arr2 = @[@2,@3,@4,@5];
        
        NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",arr1];
        NSArray * filter = [arr2 filteredArrayUsingPredicate:filterPredicate];
        NSLog(@"%@",filter);
    

    打印:

    2018-12-20 10:51:59.792762+0800 demo[2898:47742] (
        4,
        5
    )
    

    相关文章

      网友评论

          本文标题:日常更新知识点

          本文链接:https://www.haomeiwen.com/subject/ydbibftx.html