BarrageSprite.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <Foundation/Foundation.h>
  25. #import "BarrageSpriteUtility.h"
  26. #import "BarrageSpriteProtocol.h"
  27. extern NSString * const kBarrageRendererContextCanvasBounds; // 画布大小
  28. extern NSString * const kBarrageRendererContextRelatedSpirts; // 相关精灵
  29. extern NSString * const kBarrageRendererContextTimestamp; // 时间戳
  30. /// 精灵基类
  31. @interface BarrageSprite : NSObject<BarrageActionProtocol>
  32. {
  33. CGPoint _origin;
  34. BOOL _valid;
  35. CGSize _mandatorySize;
  36. NSString *_viewClassName;
  37. }
  38. /// 延时, 这个是相对于rendered的绝对时间/秒
  39. /// 如果delay<0, 当然不会提前显示 ^ - ^
  40. @property(nonatomic,assign)NSTimeInterval delay;
  41. /// 创建的绝对时间,初始化的时候创建; 目前好像没啥用处
  42. @property(nonatomic,strong,readonly)NSDate * birth;
  43. /// 时间戳,表示这个弹幕处于的时间位置;相对于时间引擎启动的时候;精灵被激活的时候生成
  44. @property(nonatomic,assign,readonly)CFTimeInterval timestamp;
  45. // 最底层是0, 往上依次叠加; 默认值是0
  46. @property(nonatomic,assign)NSUInteger z_index;
  47. /// 起始位置,为了获取这个值,子类需要重写 originInBounds:withSprites: 方法
  48. @property(nonatomic,assign,readonly)CGPoint origin;
  49. /// 为了获取此值,子类可能需要在 updateWithTime: 中修改 _position成员变量
  50. @property(nonatomic,assign,readonly)CGPoint position;
  51. /// calculate inside, return for others;如果需要不同大小的size
  52. @property(nonatomic,assign,readonly)CGSize size;
  53. /// 是否有效,默认YES; 当过了动画时间之后,就会被标记成NO; 永世不得翻身;子类可能需要在 updateWithTime: 中修改 _valid成员变量
  54. @property(nonatomic,assign,readonly,getter=isValid)BOOL valid;
  55. #pragma mark - reusableView
  56. /// 输出的view,这样就不必自己再绘制图形了,并且可以使用硬件加速
  57. @property(nonatomic,strong)UIView<BarrageViewProtocol> * view;
  58. @property(nonatomic,strong,readonly)NSString *viewClassName;
  59. @property(nonatomic,strong)NSDictionary *viewParams;
  60. /// 强制性大小,默认为CGSizeZero,大小自适应; 否则使用mandatorySize的值来设置view大小
  61. @property(nonatomic,assign)CGSize mandatorySize;
  62. #pragma mark - called, part of lifecycle
  63. /// 结合相关上下文激活精灵; 如要覆盖, 请要先调用super方法
  64. - (void)activeWithContext:(NSDictionary *)context;
  65. /// 结合相关上下文失活精灵; 如要覆盖, 请要先调用super方法
  66. - (void)deactive;
  67. /// 用相对时间更新状态; 最好不要覆盖此方法; 如要覆盖, 请要先调用super方法
  68. - (void)updateWithTime:(NSTimeInterval)time;
  69. #pragma mark - override -
  70. #pragma mark invalid&valid
  71. /// 恢复view状态,将要失效时使用
  72. - (void)restoreViewState;
  73. /// 初始化view状态
  74. - (void)initializeViewState;
  75. #pragma mark update
  76. /// _position, 此时刻的位置
  77. - (CGRect)rectWithTime:(NSTimeInterval)time;
  78. /// _valid, 此时刻是否还有效
  79. - (BOOL)validWithTime:(NSTimeInterval)time;
  80. /// 在本次弹幕周期内,强制将弹幕无效
  81. - (void)forceInvalid;
  82. #pragma mark launch
  83. /// 返回弹幕的初始位置
  84. - (CGPoint)originInBounds:(CGRect)rect withSprites:(NSArray *)sprites;
  85. @end