QGVAPWrapView.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // UIView+VAP.m
  2. // Tencent is pleased to support the open source community by making vap available.
  3. //
  4. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. //
  6. // Licensed under the MIT License (the "License"); you may not use this file except in
  7. // compliance with the License. You may obtain a copy of the License at
  8. //
  9. // http://opensource.org/licenses/MIT
  10. //
  11. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. // either express or implied. See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import "QGVAPWrapView.h"
  16. #import "QGVAPConfigModel.h"
  17. @interface QGVAPWrapView()<VAPWrapViewDelegate, HWDMP4PlayDelegate>
  18. @property (nonatomic, weak) id<VAPWrapViewDelegate> delegate;
  19. @property (nonatomic, strong) VAPView *vapView;
  20. @end
  21. @implementation QGVAPWrapView
  22. - (instancetype)init {
  23. if (self = [super init]) {
  24. [self commonInit];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithFrame:(CGRect)frame {
  29. if (self = [super initWithFrame:frame]) {
  30. [self commonInit];
  31. }
  32. return self;
  33. }
  34. - (void)commonInit {
  35. _autoDestoryAfterFinish = YES;
  36. }
  37. // 因为播放停止后可能移除VAPView,这里需要加回来
  38. - (void)initVAPViewIfNeed {
  39. if (!_vapView) {
  40. _vapView = [[VAPView alloc] initWithFrame:self.bounds];
  41. [self addSubview:_vapView];
  42. }
  43. }
  44. - (void)vapWrapView_playHWDMP4:(NSString *)filePath
  45. repeatCount:(NSInteger)repeatCount
  46. delegate:(id<VAPWrapViewDelegate>)delegate {
  47. self.delegate = delegate;
  48. [self initVAPViewIfNeed];
  49. [self.vapView playHWDMP4:filePath repeatCount:repeatCount delegate:self];
  50. }
  51. - (void)vapWrapView_addVapGesture:(UIGestureRecognizer *)gestureRecognizer callback:(VAPGestureEventBlock)handler {
  52. [self initVAPViewIfNeed];
  53. [self.vapView addVapGesture:gestureRecognizer callback:handler];
  54. }
  55. - (void)vapWrapView_addVapTapGesture:(VAPGestureEventBlock)handler {
  56. [self initVAPViewIfNeed];
  57. [self.vapView addVapTapGesture:handler];
  58. }
  59. #pragma mark - UIView
  60. // 自身不响应,仅子视图响应。
  61. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  62. if (!self.isUserInteractionEnabled || self.isHidden || self.alpha < 0.01) {
  63. return nil;
  64. }
  65. if ([self pointInside:point withEvent:event]) {
  66. for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
  67. CGPoint convertedPoint = [self convertPoint:point toView:subview];
  68. UIView *hitView = [subview hitTest:convertedPoint withEvent:event];
  69. if (hitView) {
  70. return hitView;
  71. }
  72. }
  73. return nil;
  74. }
  75. return nil;
  76. }
  77. #pragma mark - Private
  78. - (void)p_setupContentModeWithConfig:(QGVAPConfigModel *)config {
  79. CGFloat realWidth = 0.;
  80. CGFloat realHeight = 0.;
  81. CGFloat layoutWidth = self.bounds.size.width;
  82. CGFloat layoutHeight = self.bounds.size.height;
  83. CGFloat layoutRatio = self.bounds.size.width / self.bounds.size.height;
  84. CGFloat videoRatio = config.info.size.width / config.info.size.height;
  85. switch (self.contentMode) {
  86. case QGVAPWrapViewContentModeScaleToFill: {
  87. }
  88. break;
  89. case QGVAPWrapViewContentModeAspectFit: {
  90. if (layoutRatio < videoRatio) {
  91. realWidth = layoutWidth;
  92. realHeight = realWidth / videoRatio;
  93. } else {
  94. realHeight = layoutHeight;
  95. realWidth = videoRatio * realHeight;
  96. }
  97. self.vapView.frame = CGRectMake(0, 0, realWidth, realHeight);
  98. self.vapView.center = self.center;
  99. }
  100. break;;
  101. case QGVAPWrapViewContentModeAspectFill: {
  102. if (layoutRatio > videoRatio) {
  103. realWidth = layoutWidth;
  104. realHeight = realWidth / videoRatio;
  105. } else {
  106. realHeight = layoutHeight;
  107. realWidth = videoRatio * realHeight;
  108. }
  109. self.vapView.frame = CGRectMake(0, 0, realWidth, realHeight);
  110. self.vapView.center = self.center;
  111. }
  112. break;;
  113. default:
  114. break;
  115. }
  116. }
  117. #pragma mark - mp4 hwd delegate
  118. #pragma mark -- 播放流程
  119. - (void)viewDidStartPlayMP4:(VAPView *)container {
  120. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidStartPlayMP4:)]) {
  121. [self.delegate vapWrap_viewDidStartPlayMP4:container];
  122. }
  123. }
  124. - (void)viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(UIView *)container {
  125. //note:在子线程被调用
  126. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidFinishPlayMP4:view:)]) {
  127. [self.delegate vapWrap_viewDidFinishPlayMP4:totalFrameCount view:container];
  128. }
  129. }
  130. - (void)viewDidPlayMP4AtFrame:(QGMP4AnimatedImageFrame *)frame view:(UIView *)container {
  131. //note:在子线程被调用
  132. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidPlayMP4AtFrame:view:)]) {
  133. [self.delegate vapWrap_viewDidPlayMP4AtFrame:frame view:container];
  134. }
  135. }
  136. - (void)viewDidStopPlayMP4:(NSInteger)lastFrameIndex view:(UIView *)container {
  137. //note:在子线程被调用
  138. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidStopPlayMP4:view:)]) {
  139. [self.delegate vapWrap_viewDidStopPlayMP4:lastFrameIndex view:container];
  140. }
  141. dispatch_async(dispatch_get_main_queue(), ^{
  142. if (self.autoDestoryAfterFinish) {
  143. [self.vapView removeFromSuperview];
  144. self.vapView = nil;
  145. }
  146. });
  147. }
  148. - (BOOL)shouldStartPlayMP4:(VAPView *)container config:(QGVAPConfigModel *)config {
  149. [self p_setupContentModeWithConfig:config];
  150. if ([self.delegate respondsToSelector:@selector(vapWrap_viewshouldStartPlayMP4:config:)]) {
  151. return [self.delegate vapWrap_viewshouldStartPlayMP4:container config:config];
  152. }
  153. return YES;
  154. }
  155. - (void)viewDidFailPlayMP4:(NSError *)error {
  156. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidFailPlayMP4:)]) {
  157. [self.delegate vapWrap_viewDidFailPlayMP4:error];
  158. }
  159. }
  160. #pragma mark -- 融合特效的接口 vapx
  161. //provide the content for tags, maybe text or url string ...
  162. - (NSString *)contentForVapTag:(NSString *)tag resource:(QGVAPSourceInfo *)info {
  163. if ([self.delegate respondsToSelector:@selector(vapWrapview_contentForVapTag:resource:)]) {
  164. return [self.delegate vapWrapview_contentForVapTag:tag resource:info];
  165. }
  166. return nil;
  167. }
  168. //provide image for url from tag content
  169. - (void)loadVapImageWithURL:(NSString *)urlStr context:(NSDictionary *)context completion:(VAPImageCompletionBlock)completionBlock {
  170. if ([self.delegate respondsToSelector:@selector(vapWrapView_loadVapImageWithURL:context:completion:)]) {
  171. [self.delegate vapWrapView_loadVapImageWithURL:urlStr context:context completion:completionBlock];
  172. }
  173. }
  174. @end