Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions MZTimerLabel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = "<group>"; };
279565FC1E013A1700ABA8B6 /* NSTimer+Block.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSTimer+Block.m"; sourceTree = "<group>"; };
40A965111AD4CAF500B2D499 /* MZTimerLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MZTimerLabel.h; sourceTree = "<group>"; };
40A965121AD4CAF500B2D499 /* MZTimerLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MZTimerLabel.m; sourceTree = "<group>"; };
40A9651A1AD4CB5F00B2D499 /* MZTimerLabel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MZTimerLabel.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -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 */,
Expand All @@ -90,6 +96,7 @@
buildActionMask = 2147483647;
files = (
40A965371AD4CDB700B2D499 /* MZTimerLabel.h in Headers */,
279565FD1E013A1700ABA8B6 /* NSTimer+Block.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -188,6 +195,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
279565FE1E013A1700ABA8B6 /* NSTimer+Block.m in Sources */,
40A965381AD4CDBD00B2D499 /* MZTimerLabel.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
36 changes: 32 additions & 4 deletions MZTimerLabel/MZTimerLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// THE SOFTWARE.

#import "MZTimerLabel.h"

#import "NSTimer+Block.h"

#define kDefaultTimeFormat @"HH:mm:ss"
#define kHourFormatReplace @"!!!*"
Expand Down Expand Up @@ -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];

Expand Down
23 changes: 23 additions & 0 deletions MZTimerLabel/NSTimer+Block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// NSTimer+Block.h
// SuperCalculator
//
// Created by Peng Wang on 2016/12/14.
// Copyright © 2016年 youdao. All rights reserved.
//

#import <Foundation/Foundation.h>


/**
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
30 changes: 30 additions & 0 deletions MZTimerLabel/NSTimer+Block.m
Original file line number Diff line number Diff line change
@@ -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