BarrageSprite.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "BarrageSprite.h"
  25. #import "BarrageViewPool.h"
  26. @interface BarrageSprite()
  27. @property(nonatomic,strong)UITapGestureRecognizer *tapGestureRecognizer;
  28. @property(nonatomic,assign)BOOL forcedInvalid;
  29. @end
  30. @implementation BarrageSprite
  31. @synthesize mandatorySize = _mandatorySize;
  32. @synthesize clickAction = _clickAction;
  33. @synthesize origin = _origin;
  34. @synthesize valid = _valid;
  35. @synthesize view = _view;
  36. @synthesize viewClassName = _viewClassName;
  37. - (instancetype)init
  38. {
  39. if (self = [super init]) {
  40. _delay = 0.0f;
  41. _birth = [NSDate date];
  42. _valid = YES;
  43. _origin.x = _origin.y = MAXFLOAT;
  44. _z_index = 0;
  45. _forcedInvalid = NO;
  46. _mandatorySize = CGSizeZero;
  47. _viewClassName = NSStringFromClass([UIView class]);
  48. }
  49. return self;
  50. }
  51. #pragma mark - update
  52. - (void)updateWithTime:(NSTimeInterval)time
  53. {
  54. _valid = !self.forcedInvalid && [self validWithTime:time];
  55. _view.frame = [self rectWithTime:time];
  56. if ([_view respondsToSelector:@selector(updateWithTime:)]) {
  57. [_view updateWithTime:time];
  58. }
  59. }
  60. - (CGRect)rectWithTime:(NSTimeInterval)time
  61. {
  62. return CGRectMake(_origin.x, _origin.y, self.size.width, self.size.height);
  63. }
  64. - (BOOL)validWithTime:(NSTimeInterval)time
  65. {
  66. return YES;
  67. }
  68. - (void)forceInvalid
  69. {
  70. self.forcedInvalid = YES;
  71. }
  72. #pragma mark - active and deactive
  73. - (void)activeWithContext:(NSDictionary *)context
  74. {
  75. CGRect rect = [[context objectForKey:kBarrageRendererContextCanvasBounds]CGRectValue];
  76. NSArray * sprites = [context objectForKey:kBarrageRendererContextRelatedSpirts];
  77. NSTimeInterval timestamp = [[context objectForKey:kBarrageRendererContextTimestamp]doubleValue];
  78. _timestamp = timestamp;
  79. [[BarrageViewPool mainPool]assembleBarrageViewForSprite:self];
  80. [self initializeViewState];
  81. [self.view sizeToFit];
  82. if (!CGSizeEqualToSize(_mandatorySize, CGSizeZero)) {
  83. self.view.frame = CGRectMake(0, 0, _mandatorySize.width, _mandatorySize.height);
  84. }
  85. _origin = [self originInBounds:rect withSprites:sprites];
  86. self.view.frame = CGRectMake(_origin.x, _origin.y, self.size.width, self.size.height);
  87. }
  88. - (void)deactive
  89. {
  90. [self restoreViewState];
  91. self.forcedInvalid = NO;
  92. [[BarrageViewPool mainPool]reclaimBarrageViewForSprite:self];
  93. }
  94. /// 恢复view状态,初始化view时使用
  95. - (void)restoreViewState
  96. {
  97. if (self.clickAction) {
  98. self.view.userInteractionEnabled = NO;
  99. [self.view removeGestureRecognizer:self.tapGestureRecognizer];
  100. }
  101. }
  102. - (void)initializeViewState
  103. {
  104. self.view.frame = CGRectZero;
  105. [self.view configureWithParams:self.viewParams];
  106. if (self.clickAction) {
  107. _view.userInteractionEnabled = YES;
  108. [_view addGestureRecognizer:self.tapGestureRecognizer];
  109. }
  110. }
  111. #pragma mark - gesture
  112. - (UIGestureRecognizer *)tapGestureRecognizer
  113. {
  114. if (!_tapGestureRecognizer) {
  115. _tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickSpriteView)];
  116. }
  117. return _tapGestureRecognizer;
  118. }
  119. - (void)clickSpriteView
  120. {
  121. if (self.clickAction) self.clickAction(self.viewParams);
  122. }
  123. /// 区域内的初始位置,只在刚加入渲染器的时候被调用;子类继承需要override.
  124. - (CGPoint)originInBounds:(CGRect)rect withSprites:(NSArray *)sprites
  125. {
  126. CGFloat x = random_between(rect.origin.x, rect.origin.x+rect.size.width-self.size.width);
  127. CGFloat y = random_between(rect.origin.y, rect.origin.y+rect.size.height-self.size.height);
  128. return CGPointMake(x, y);
  129. }
  130. #pragma mark - attributes
  131. - (void)setClickAction:(BarrageClickAction)clickAction
  132. {
  133. _clickAction = [clickAction copy];
  134. }
  135. - (CGPoint)position
  136. {
  137. return self.view.frame.origin;
  138. }
  139. - (CGSize)size
  140. {
  141. return self.view.bounds.size;
  142. }
  143. - (void)setValue:(id)value forUndefinedKey:(NSString *)key
  144. {
  145. #ifdef DEBUG
  146. // MOLogV(@"[Class:%@] hasNo - [Property:%@]; [Value:%@] will be discarded.",NSStringFromClass([self class]),key,value);
  147. #endif
  148. }
  149. @end