zoukankan      html  css  js  c++  java
  • Object-C知识点 (三) 单例 蒙版 刷新 KVO底层

    #pragma mark - 单例方法(完整的方法)
    系统的单例方法名称 sharedApplication defaultManager standardUserDefaults currentDevice

    [UIApplication sharedApplication];
    
    [NSFileManager defaultManager];
    
    [NSUserDefaults standardUserDefaults];
    
    [UIDevice currentDevice];


    + (instancetype)sharedInstace;
    
    // 用来保存唯一的单例对象
    static id _instace;
    
    + (id)allocWithZone:(struct _NSZone *)zone
    {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instace = [super allocWithZone:zone];
    });
    return _instace;
    }
    
    + (instancetype)sharedInstace
    {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instace = [[self alloc] init];
    });
    return _instace;
    }
    
    - (id)copyWithZone:(NSZone *)zone
    {
    return _instace;
    }
    
    - (id)retain { return self; }
    - (oneway void)release { }
    - (id)autorelease { return self;}
    - (NSUInteger)retainCount { return 1;}

    #pragma mark--蒙板(遮罩层)

    //1、添加蒙板(遮罩)
    UIButton * coverButton = [[UIButton alloc] initWithFrame:self.view.bounds];

    coverButton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];

    [coverButton addTarget:self action:@selector(tapcoverButton:) forControlEvents:UIControlEventTouchUpInside];

    coverButton.alpha = 0.0;

    [self.view addSubview:coverButton];

    //2、将需要放大的图像按钮弄到最前面
    //bringSubviewToFront
    [self.view bringSubviewToFront:self.iconButton];

    //3、动画放大图像按钮
    CGFloat width = self.view.bounds.size.width;

    CGFloat height = width;

    CGFloat y = (self.view.bounds.size.height - height) * 0.5;

    [UIView animateWithDuration:1.0 animations:^{

    self.iconButton.frame = CGRectMake(0, y, width, height);

    coverButton.alpha = 1.0;
    }];

    [UIView animateWithDuration:1.0 animations:^{

    //将图像恢复初始位置
    self.iconButton.frame = CGRectMake(112, 125, 150, 150);

    button.alpha = 0.0;
    } completion:^(BOOL finished) {

    //动画完成之后,删除cover
    [button removeFromSuperview];
    }]; 

    #pragma mark -- 判断一个路径里面的文件总大小(NSString的分类)


    - (NSInteger)fileSize
    {
        NSFileManager *mgr = [NSFileManager defaultManager];
        
        // 判断是否为文件夹
        BOOL dir = NO;
        BOOL exists = [mgr fileExistsAtPath:self isDirectory:&dir];
        
        // 文件文件夹不存在
        if (exists == NO) return 0;
        
        if (dir) { // self是一个文件夹
            // 遍历caches里面的所有内容 --- 直接和间接内容
            NSArray *subpaths = [mgr subpathsAtPath:self];
            NSInteger totalByteSize = 0;
            
            for (NSString *subpath in subpaths) {
                // 获得全路径
                NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
                
                // 判断是否为文件夹 subpath为系统返回的路径,肯定存在文件夹或者文件
                BOOL dir = NO;
                [mgr fileExistsAtPath:fullSubpath isDirectory:&dir];
                
                if (dir == NO) { // 不是文件夹,是文件
                    totalByteSize += [[mgr attributesOfItemAtPath:fullSubpath error:nil][NSFileSize] integerValue];
                }
            }
            return totalByteSize;
        } else { // self是一个文件
            return [[mgr attributesOfItemAtPath:self error:nil][NSFileSize] integerValue];
        }
    }

    #pragma mark - 自己封装刷新控件

    KVO 监听父视图的contentOffset
    scrollView.addObserver(self, forKeyPath: "contentOffset", options: [], context: nil)

    // 本视图从父视图上移除
    // 提示:所有的下拉刷新框架都是监听父视图的 contentOffset
    // 所有的框架的 KVO 监听实现思路都是这个!
    override func removeFromSuperview() {
    // superView 还存在
    superview?.removeObserver(self, forKeyPath: "contentOffset")

    super.removeFromSuperview() //在这句话之前移除监听

    // superView 不存在
    }


    // 观察者模式,在不需要的时候,都需要释放
    // - 通知中心:如果不释放,什么也不会发生,但是会有内存泄漏,会有多次注册的可能!
    // - KVO:如果不释放,会崩溃!


    #pragma mark - KVO底层实现原理

    // 利用KVO可以很简单的实现监听对象的属性发生改变,KVO的底层: "重写setter方法"

    // 结论:只有触发了setter方法才会触发KVO的监听

    // 底层实现如下:

    // 1.动态创建NSKVONotifying_Person,NSKVONotifying_Person是Person子类,利用NSKVONotifying_Person做KVO的实现
    // 2.修改当前对象的isa指针->NSKVONotifying_Person
    // 3.只要调用对象的set,就会调用NSKVONotifying_Person的setter方法
    // 4.重写NSKVONotifying_Person的setter方法
    //    4.1 [super set:]
    //    4.2 通知观察者,告诉你属性改变

    连接--> KVO底层实现原理,仿写KVO

    #pragma mark - 头文件标志的含义


    C : 类NSString
    f : NSObjCRuntime
    T : 基本数据类型NSInteger/结构体 NSRange
    V : 字符串常量
    k : 枚举

     更多内容--> 博客导航 每周一篇哟!!!

    有任何关于iOS开发的问题!欢迎下方留言!!!或者邮件lieryangios@126.com 虽然我不一定能够解答出来,但是我会请教iOS开发高手!!!解答您的问题!!!

  • 相关阅读:
    图片合成
    ASP.net常用对象之一(Request对象)
    vs2010新增功能
    ASP.NET MVC 入门5、View与ViewData【转】
    ASP.NET MVC 入门3、Routing【转】
    ASP.NET MVC 入门2、项目的目录结构与核心的DLL[转]
    ASP.NET MVC 入门4、Controller与Action【转】
    jquery相关文摘
    application技术整理
    vb datagrid中的欄目順序要與recordset的順序一致
  • 原文地址:https://www.cnblogs.com/CoderEYLee/p/Object-C-0009.html
Copyright © 2011-2022 走看看