diff --git a/MZTimerLabel.xcodeproj/project.pbxproj b/MZTimerLabel.xcodeproj/project.pbxproj index c269ade..7d8c54e 100644 --- a/MZTimerLabel.xcodeproj/project.pbxproj +++ b/MZTimerLabel.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 279565FD1E013A1700ABA8B6 /* NSTimer+Block.h in Headers */ = {isa = PBXBuildFile; fileRef = 279565FB1E013A1700ABA8B6 /* NSTimer+Block.h */; }; + 279565FE1E013A1700ABA8B6 /* NSTimer+Block.m in Sources */ = {isa = PBXBuildFile; fileRef = 279565FC1E013A1700ABA8B6 /* NSTimer+Block.m */; }; 40A965371AD4CDB700B2D499 /* MZTimerLabel.h in Headers */ = {isa = PBXBuildFile; fileRef = 40A965111AD4CAF500B2D499 /* MZTimerLabel.h */; settings = {ATTRIBUTES = (Public, ); }; }; 40A965381AD4CDBD00B2D499 /* MZTimerLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 40A965121AD4CAF500B2D499 /* MZTimerLabel.m */; }; /* End PBXBuildFile section */ @@ -22,6 +24,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 279565FB1E013A1700ABA8B6 /* NSTimer+Block.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSTimer+Block.h"; sourceTree = ""; }; + 279565FC1E013A1700ABA8B6 /* NSTimer+Block.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSTimer+Block.m"; sourceTree = ""; }; 40A965111AD4CAF500B2D499 /* MZTimerLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MZTimerLabel.h; sourceTree = ""; }; 40A965121AD4CAF500B2D499 /* MZTimerLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MZTimerLabel.m; sourceTree = ""; }; 40A9651A1AD4CB5F00B2D499 /* MZTimerLabel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MZTimerLabel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -67,6 +71,8 @@ 40A9651C1AD4CB5F00B2D499 /* MZTimerLabel */ = { isa = PBXGroup; children = ( + 279565FB1E013A1700ABA8B6 /* NSTimer+Block.h */, + 279565FC1E013A1700ABA8B6 /* NSTimer+Block.m */, 40A965111AD4CAF500B2D499 /* MZTimerLabel.h */, 40A965121AD4CAF500B2D499 /* MZTimerLabel.m */, 40A9651D1AD4CB5F00B2D499 /* Supporting Files */, @@ -90,6 +96,7 @@ buildActionMask = 2147483647; files = ( 40A965371AD4CDB700B2D499 /* MZTimerLabel.h in Headers */, + 279565FD1E013A1700ABA8B6 /* NSTimer+Block.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -188,6 +195,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 279565FE1E013A1700ABA8B6 /* NSTimer+Block.m in Sources */, 40A965381AD4CDBD00B2D499 /* MZTimerLabel.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/MZTimerLabel/MZTimerLabel.m b/MZTimerLabel/MZTimerLabel.m index ee33133..63768bc 100644 --- a/MZTimerLabel/MZTimerLabel.m +++ b/MZTimerLabel/MZTimerLabel.m @@ -27,7 +27,7 @@ // THE SOFTWARE. #import "MZTimerLabel.h" - +#import "NSTimer+Block.h" #define kDefaultTimeFormat @"HH:mm:ss" #define kHourFormatReplace @"!!!*" @@ -243,10 +243,38 @@ -(void)start{ _timer = nil; } - if ([self.timeFormat rangeOfString:@"SS"].location != NSNotFound) { - _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalHighUse target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; + CGFloat precision; + + if ([self.timeFormat rangeOfString:@"SS"].location != NSNotFound){ + + precision = kDefaultFireIntervalHighUse; + }else{ + + precision = kDefaultFireIntervalNormal; + } + + __weak typeof(self)weakself = self; + + if ([NSTimer respondsToSelector:@selector(scheduledTimerWithTimeInterval:repeats:block:)]) { + + _timer = [NSTimer scheduledTimerWithTimeInterval:precision + repeats:YES + block:^(NSTimer * _Nonnull timer) { + __strong typeof(weakself)strongself = weakself; + if (strongself) { + [strongself updateLabel]; + } + }]; }else{ - _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalNormal target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; + + _timer = [NSTimer scheduledTimerWithTimeInterval:precision + block:^{ + __strong typeof(weakself)strongself = weakself; + if (strongself) { + [strongself updateLabel]; + } + } + repeats:YES]; } [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; diff --git a/MZTimerLabel/NSTimer+Block.h b/MZTimerLabel/NSTimer+Block.h new file mode 100644 index 0000000..541c139 --- /dev/null +++ b/MZTimerLabel/NSTimer+Block.h @@ -0,0 +1,23 @@ +// +// NSTimer+Block.h +// SuperCalculator +// +// Created by Peng Wang on 2016/12/14. +// Copyright © 2016年 youdao. All rights reserved. +// + +#import + + +/** + NSTimer (Block) is used to solving retain cycle below ios10.0 + + if is ios10.0 ,we can use method: + + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)); + + */ +@interface NSTimer (Block) + ++ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats; + +@end diff --git a/MZTimerLabel/NSTimer+Block.m b/MZTimerLabel/NSTimer+Block.m new file mode 100644 index 0000000..45ce4fe --- /dev/null +++ b/MZTimerLabel/NSTimer+Block.m @@ -0,0 +1,30 @@ +// +// NSTimer+Block.m +// SuperCalculator +// +// Created by Peng Wang on 2016/12/14. +// Copyright © 2016年 youdao. All rights reserved. +// + +#import "NSTimer+Block.h" + +@implementation NSTimer (Block) + ++ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats{ + + return [self scheduledTimerWithTimeInterval:interval + target:self + selector:@selector(blockInvoke:) + userInfo:[block copy] + repeats:repeats]; +} + ++ (void)blockInvoke:(NSTimer*)timer { + + void (^block)() = timer.userInfo; + if (block) { + block(); + } +} + +@end