YYAsyncLayer.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // YYAsyncLayer.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 15/4/11.
  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 <UIKit/UIKit.h>
  12. #import <QuartzCore/QuartzCore.h>
  13. @class YYAsyncLayerDisplayTask;
  14. NS_ASSUME_NONNULL_BEGIN
  15. /**
  16. The YYAsyncLayer class is a subclass of CALayer used for render contents asynchronously.
  17. @discussion When the layer need update it's contents, it will ask the delegate
  18. for a async display task to render the contents in a background queue.
  19. */
  20. @interface YYAsyncLayer : CALayer
  21. /// Whether the render code is executed in background. Default is YES.
  22. @property BOOL displaysAsynchronously;
  23. @end
  24. /**
  25. The YYAsyncLayer's delegate protocol. The delegate of the YYAsyncLayer (typically a UIView)
  26. must implements the method in this protocol.
  27. */
  28. @protocol YYAsyncLayerDelegate <NSObject>
  29. @required
  30. /// This method is called to return a new display task when the layer's contents need update.
  31. - (YYAsyncLayerDisplayTask *)newAsyncDisplayTask;
  32. @end
  33. /**
  34. A display task used by YYAsyncLayer to render the contents in background queue.
  35. */
  36. @interface YYAsyncLayerDisplayTask : NSObject
  37. /**
  38. This block will be called before the asynchronous drawing begins.
  39. It will be called on the main thread.
  40. block param layer: The layer.
  41. */
  42. @property (nullable, nonatomic, copy) void (^willDisplay)(CALayer *layer);
  43. /**
  44. This block is called to draw the layer's contents.
  45. @discussion This block may be called on main thread or background thread,
  46. so is should be thread-safe.
  47. block param context: A new bitmap content created by layer.
  48. block param size: The content size (typically same as layer's bound size).
  49. block param isCancelled: If this block returns `YES`, the method should cancel the
  50. drawing process and return as quickly as possible.
  51. */
  52. @property (nullable, nonatomic, copy) void (^display)(CGContextRef context, CGSize size, BOOL(^isCancelled)(void));
  53. /**
  54. This block will be called after the asynchronous drawing finished.
  55. It will be called on the main thread.
  56. block param layer: The layer.
  57. block param finished: If the draw process is cancelled, it's `NO`, otherwise it's `YES`;
  58. */
  59. @property (nullable, nonatomic, copy) void (^didDisplay)(CALayer *layer, BOOL finished);
  60. @end
  61. NS_ASSUME_NONNULL_END