YYTimer.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // YYTimer.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 15/2/7.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <Foundation/Foundation.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. YYTimer is a thread-safe timer based on GCD. It has similar API with `NSTimer`.
  15. YYTimer object differ from NSTimer in a few ways:
  16. * It use GCD to produce timer tick, and won't be affected by runLoop.
  17. * It make a weak reference to the target, so it can avoid retain cycles.
  18. * It always fire on main thread.
  19. */
  20. @interface YYTimer : NSObject
  21. + (YYTimer *)timerWithTimeInterval:(NSTimeInterval)interval
  22. target:(id)target
  23. selector:(SEL)selector
  24. repeats:(BOOL)repeats;
  25. + (YYTimer *)timerWithFireTime:(NSTimeInterval)start
  26. timeInterval:(NSTimeInterval)interval
  27. target:(id)target
  28. selector:(SEL)selector
  29. repeats:(BOOL)repeats;
  30. - (instancetype)initWithFireTime:(NSTimeInterval)start
  31. interval:(NSTimeInterval)interval
  32. target:(id)target
  33. selector:(SEL)selector
  34. repeats:(BOOL)repeats NS_DESIGNATED_INITIALIZER;
  35. @property (readonly) BOOL repeats;
  36. @property (readonly) NSTimeInterval timeInterval;
  37. @property (readonly, getter=isValid) BOOL valid;
  38. - (void)invalidate;
  39. - (void)fire;
  40. @end
  41. NS_ASSUME_NONNULL_END