forked from libobjc/SGPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// SGRenderTimer.h | ||
// SGPlayer | ||
// | ||
// Created by Single on 2019/6/28. | ||
// Copyright © 2019 single. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface SGRenderTimer : NSObject | ||
|
||
- (instancetype)initWithHandler:(dispatch_block_t)handler; | ||
|
||
@property (nonatomic) NSTimeInterval timeInterval; | ||
@property (nonatomic) BOOL paused; | ||
|
||
- (void)start; | ||
- (void)stop; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// SGRenderTimer.m | ||
// SGPlayer | ||
// | ||
// Created by Single on 2019/6/28. | ||
// Copyright © 2019 single. All rights reserved. | ||
// | ||
|
||
#import "SGRenderTimer.h" | ||
|
||
@interface SGRenderTimer () | ||
|
||
@property (nonatomic, copy) dispatch_block_t handler; | ||
@property (nonatomic, strong) NSTimer *timer; | ||
|
||
@end | ||
|
||
@implementation SGRenderTimer | ||
|
||
- (instancetype)initWithHandler:(dispatch_block_t)handler | ||
{ | ||
if (self = [super init]) { | ||
self.handler = handler; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[self stop]; | ||
} | ||
|
||
- (void)timerHandler | ||
{ | ||
if (self.handler) { | ||
self.handler(); | ||
} | ||
} | ||
|
||
- (void)setTimeInterval:(NSTimeInterval)timeInterval | ||
{ | ||
if (self->_timeInterval != timeInterval) { | ||
self->_timeInterval = timeInterval; | ||
[self start]; | ||
} | ||
} | ||
|
||
- (void)setPaused:(BOOL)paused | ||
{ | ||
if (self->_paused != paused) { | ||
self->_paused = paused; | ||
[self fire]; | ||
} | ||
} | ||
|
||
- (void)start | ||
{ | ||
[self stop]; | ||
self->_timer = [NSTimer timerWithTimeInterval:self->_timeInterval | ||
target:self | ||
selector:@selector(timerHandler) | ||
userInfo:nil | ||
repeats:YES]; | ||
[[NSRunLoop mainRunLoop] addTimer:self->_timer | ||
forMode:NSRunLoopCommonModes]; | ||
[self fire]; | ||
} | ||
|
||
- (void)stop | ||
{ | ||
[self->_timer invalidate]; | ||
self->_timer = nil; | ||
} | ||
|
||
- (void)fire | ||
{ | ||
self->_timer.fireDate = | ||
self->_paused ? | ||
[NSDate distantFuture] : | ||
[NSDate distantPast]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters