属性锁TimeLock

时间:2022-08-08 14:38:28

属性锁TimeLock

属性锁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