• 属性锁TimeLock


    属性锁TimeLock

    这是本人所写的几个类,用于对属性设置值时进行锁定与解锁,适用于高逼格动画当中。

    源码:

    TimeLock.h 与 TimeLock.m

    //
    //  TimeLock.h
    //  TimeLock
    //
    //  Created by YouXianMing on 14/12/3.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    typedef void (^timeLockBlock)();
    
    @interface TimeLock : NSObject
    
    @property (nonatomic, assign) BOOL lock;
    
    @end
    //
    //  TimeLock.m
    //  TimeLock
    //
    //  Created by YouXianMing on 14/12/3.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "TimeLock.h"
    
    @implementation TimeLock
    
    @end

    延时锁:

    TimeDelayLock.h 与 TimeDelayLock.m

    //
    //  TimeDelayLock.h
    //  TimeLock
    //
    //  Created by YouXianMing on 14/12/3.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "TimeLock.h"
    #import <UIKit/UIKit.h>
    
    @interface TimeDelayLock : TimeLock
    
    /**
     *  延时的时间
     */
    @property (nonatomic, assign) CGFloat duration;
    
    /**
     *  执行上锁的block
     *
     *  @param block 可执行代码
     */
    - (void)excuteBlock:(timeLockBlock)block;
    
    @end

    仅运行一次的锁:

    TimeLockOnce.h 与 TimeLockOnce.m

    //
    //  TimeLockOnce.h
    //  TimeLock
    //
    //  Created by YouXianMing on 14/12/3.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "TimeLock.h"
    
    @interface TimeLockOnce : TimeLock
    
    /**
     *  执行上锁的block
     *
     *  @param block 可执行代码
     */
    - (void)excuteBlock:(timeLockBlock)block;
    
    @end
    //
    //  TimeLockOnce.m
    //  TimeLock
    //
    //  Created by YouXianMing on 14/12/3.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "TimeLockOnce.h"
    
    @implementation TimeLockOnce
    
    - (void)excuteBlock:(timeLockBlock)block {
        if (self.lock == NO) {
            self.lock = YES;
            
            block();
        }
    }
    
    @end

    使用时候的详情:

    //
    //  Model.h
    //  TimeLock
    //
    //  Created by YouXianMing on 14/12/3.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Model : NSObject
    
    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, strong) NSString *sex;
    
    @end
    //
    //  Model.m
    //  TimeLock
    //
    //  Created by YouXianMing on 14/12/3.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "Model.h"
    #import "TimeLockOnce.h"
    #import "TimeDelayLock.h"
    
    @interface Model ()
    
    @property (nonatomic, strong) TimeDelayLock *delayLock;
    @property (nonatomic, strong) TimeLockOnce  *lockOnce;
    
    @end
    
    @implementation Model
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            self.delayLock = [TimeDelayLock new];
            self.lockOnce  = [TimeLockOnce new];
        }
        return self;
    }
    
    @synthesize name = _name;
    - (void)setName:(NSString *)name {
        [self.delayLock excuteBlock:^{
            _name = name;
        }];
    }
    - (NSString *)name {
        return _name;
    }
    
    @synthesize sex = _sex;
    - (void)setSex:(NSString *)sex {
        [self.lockOnce excuteBlock:^{
            _sex = sex;
        }];
    }
    - (NSString *)sex {
        return _sex;
    }
    
    @end
  • 相关阅读:
    linux下的exec命令
    jenkins+gitlab+maven+docker部署项目之jenkins用户权限管理
    油候插件grant的使用
    python deepcopy的替代方案
    starletter代码示例
    mac使用pytorch
    Mac ERROR:root:code for hash md5 was not found.
    scrapy-redis分布式爬虫实战
    mac进行redis5.0单机集群笔记
    合并两个有序的链表Python和Go代码
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4141545.html
Copyright © 2020-2023  润新知