BarrageClock.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Part of BarrageRenderer. Created by UnAsh.
  2. // Blog: http://blog.exbye.com
  3. // Github: https://github.com/unash/BarrageRenderer
  4. // This code is distributed under the terms and conditions of the MIT license.
  5. // Copyright (c) 2015年 UnAsh.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "BarrageClock.h"
  25. @interface BarrageClock()
  26. {
  27. void (^_block)(NSTimeInterval time);
  28. CADisplayLink * _displayLink; // 周期
  29. CFTimeInterval _previousDate; // 上一次更新时间
  30. CGFloat _pausedSpeed; // 暂停之前的时间流速
  31. }
  32. ///是否处于启动状态
  33. @property(nonatomic,assign)BOOL launched;
  34. ///逻辑时间
  35. @property(nonatomic,assign)NSTimeInterval time;
  36. @end
  37. @implementation BarrageClock
  38. + (instancetype)clockWithHandler:(void (^)(NSTimeInterval time))block
  39. {
  40. BarrageClock * clock = [[BarrageClock alloc]initWithHandler:block];
  41. return clock;
  42. }
  43. - (instancetype)initWithHandler:(void (^)(NSTimeInterval time))block
  44. {
  45. if (self = [super init]) {
  46. _block = block;
  47. }
  48. return self;
  49. }
  50. - (void)reset
  51. {
  52. _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
  53. _speed = 1.0f;
  54. _pausedSpeed = _speed;
  55. self.launched = NO;
  56. }
  57. - (void)update
  58. {
  59. [self updateTime];
  60. _block(self.time);
  61. }
  62. - (void)start
  63. {
  64. if (!_displayLink) {
  65. [self reset];
  66. }
  67. if (self.launched) {
  68. _speed = _pausedSpeed;
  69. } else {
  70. _previousDate = CACurrentMediaTime();
  71. [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
  72. self.launched = YES;
  73. }
  74. }
  75. - (void)setSpeed:(CGFloat)speed
  76. {
  77. if (speed > 0.0f) {
  78. if (_speed != 0.0f) { // 非暂停状态
  79. _speed = speed;
  80. }
  81. _pausedSpeed = speed;
  82. }
  83. }
  84. - (void)pause
  85. {
  86. _speed = 0.0f;
  87. }
  88. - (void)stop
  89. {
  90. [_displayLink invalidate];
  91. _displayLink = nil;
  92. }
  93. /// 更新逻辑时间系统
  94. - (void)updateTime
  95. {
  96. CFTimeInterval currentDate = CACurrentMediaTime();
  97. self.time += (currentDate - _previousDate) * self.speed;
  98. _previousDate = currentDate;
  99. }
  100. @end