美文网首页
iOS 日常常用随笔

iOS 日常常用随笔

作者: Pierce_蛋 | 来源:发表于2019-02-27 16:34 被阅读0次

    获取cell 在collection view 中的位置

    //求得出点击的cell在当前View的位置
    UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:indexPath];
     
    CGPoint cellCenter = attributes.center;
    CGPoint anchorPoint = [collectionView convertPoint:cellCenter toView:self.view];
    

    NSLog的输出格式

    %@                   对象
    %d, %i               整数
    %u                    无符整形
    %f                     浮点/双字
    %x, %X              二进制整数
    %o                    八进制整数
    %zu size_t
    %p                    指针
    %e                    浮点/双字 (科学计算)
    %g                    浮点/双字
    %s C                字符串
    %.*s                  Pascal字符串
    %c                    字符
    %C                    unichar
    %lld                   64位长整数(long long)
    %llu                   无符64位长整数
    %Lf                    64位双字
    

    通知的使用

    //创建通知
        [[NSNotificationCenter defaultCenter] postNotificationName:kMusicPlaybackStateChangeNotification object:musicIdentifier];
    
    //监听通知,其中notificationMethod为通知的执行方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationMethod:) name:kMusicPlaybackStateChangeNotification object:nil];
    
    //销毁通知
    - (void)dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    

    mac 中一些资源路径

    Xcode 中安装的证书的路径:~/Library/MobileDevice/Provisioning Profiles
    

    iOS 获取汉字的拼音

    + (NSString *)transform:(NSString *)chinese {
    
        //将NSString装换成NSMutableString
        NSMutableString *pinyin = [chinese mutableCopy];
        //将汉字转换为拼音(带音标)
        CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
        NSLog(@"%@", pinyin);
        //去掉拼音的音标
        CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
        NSLog(@"%@", pinyin);
        //返回最近结果
        return pinyin;
    }
    

    关于NSDateFormatter的格式

    G: 公元时代,例如AD公元
     yy: 年的后2位
     yyyy: 完整年
     MM: 月,显示为1-12
     MMM: 月,显示为英文月份简写,如 Jan
     MMMM: 月,显示为英文月份全称,如 Janualy
     dd: 日,2位数表示,如02 
    d: 日,1-2位显示,如 2 
    EEE: 简写星期几,如Sun
    EEEE: 全写星期几,如Sunday
    aa: 上下午,AM/PM 
    H: 时,24小时制,0-23 
    K:时,12小时制,0-11 
    m: 分,1-2位 
    mm: 分,2位 
    s: 秒,1-2位 
    ss: 秒,2位 
    S: 毫秒
    

    view的部分圆角

    - (void)cornerForView:(UIView *)view byRoundingCorners:(UIRectCorner)corners {
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                           byRoundingCorners:corners
                                                                 cornerRadii:CGSizeMake(8, 8)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = view.bounds;
            maskLayer.path = maskPath.CGPath;
            view.layer.mask = maskLayer;
        }
    

    判断tableview是否reload完成

    //下面代码逻辑中为什么可以标识tableview是否reload完成
    
    dispatch_async(dispatch_get_main_queue(), ^{
        _isReloadDone = NO;
        [tableView reload]; //会自动设置tableView layoutIfNeeded为YES,意味着将会在runloop结束时重绘table
        dispatch_async(dispatch_get_main_queue(),^{
            _isReloadDone = YES;
        });
    });
    
    //提示:这里在GCD dispatch main queue中插入了两个任务,
    //一次RunLoop有两个机会执行GCD dispatch main queue中的任务,
    //分别在休眠前和被唤醒后。
    

    如何播放音效?

    // 1.声明音效ID
    SystemSoundID _clickSoundID; 
    // 2.获取音频路径
     NSString *clickSoundPath = [[NSBundle mainBundle] pathForResource:@"click" ofType:@"wav"];
    // 3.创建音频服务
     AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:clickSoundPath], &_clickSoundID);
    // 4.播放音频文件
    AudioServicesPlaySystemSound (_clickSoundID);
    

    相关文章

      网友评论

          本文标题:iOS 日常常用随笔

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