PlayerView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. //
  2. // PlayerView.m
  3. // VclustersGemini
  4. //
  5. // Created by APPLE on 2019/6/13.
  6. // Copyright © 2019 APPLE. All rights reserved.
  7. //
  8. #import "PlayerView.h"
  9. #import "UIView+View.h"
  10. @interface PlayerView ()
  11. <UIGestureRecognizerDelegate,
  12. ShowImageViewDelegate>{
  13. EAGLContext *eagContext;
  14. CIContext *ciContext;
  15. CIFilter *filter;
  16. //触控指令视图,用于计算触控位置坐标
  17. ShowImageView *touchCommondView;
  18. UIView *bottomContrView;
  19. UIButton *mueBtn;
  20. UIButton *homeBtn;
  21. UIButton *backBtn;
  22. }
  23. @end
  24. @implementation PlayerView
  25. @synthesize showImageView;
  26. @synthesize glkshowImageView;
  27. @synthesize controlBtn;
  28. @synthesize delegate;
  29. @synthesize isLan;
  30. - (id)initWithFrame:(CGRect)frame{
  31. self = [super initWithFrame:frame];
  32. if (self){
  33. [self setBackgroundColor:[UIColor blackColor]];
  34. [self initOpenGL];
  35. [self drawView:frame];
  36. }
  37. return self;
  38. }
  39. - (void)initOpenGL{
  40. eagContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  41. ciContext = [CIContext contextWithEAGLContext:eagContext
  42. options:@{kCIContextWorkingColorSpace:[NSNull null]}];
  43. filter = [CIFilter filterWithName:@"CISepiaTone"];
  44. }
  45. - (void)glkShowImage:(CIImage *)ciImage{
  46. [filter setValue:ciImage forKey:kCIInputImageKey];
  47. [filter setValue:@(0) forKey:kCIInputIntensityKey];
  48. //开始渲染
  49. [ciContext drawImage:[filter valueForKey:kCIOutputImageKey]
  50. inRect:CGRectMake(0, 0, glkshowImageView.drawableWidth, glkshowImageView.drawableHeight)
  51. fromRect:[ciImage extent]];
  52. //[self->glkshowImageView display];
  53. }
  54. - (void)showIMage
  55. {
  56. ;
  57. }
  58. - (void)drawView:(CGRect)frame{
  59. self.frame = frame;
  60. touchCommondView = [[ShowImageView alloc] init];
  61. [touchCommondView setBackgroundColor: [UIColor clearColor]];
  62. [touchCommondView setUserInteractionEnabled:YES];
  63. touchCommondView.multipleTouchEnabled = YES;
  64. touchCommondView.delegate = self;
  65. [self addSubview:touchCommondView];
  66. [touchCommondView mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.left.mas_equalTo(0.f);
  68. make.height.mas_equalTo(frame.size.height);
  69. make.right.mas_equalTo(0.f);
  70. make.top.mas_equalTo(0.f);
  71. }];
  72. /*播放视图*/
  73. showImageView = [[ShowImageView alloc] init];
  74. [showImageView setBackgroundColor: [UIColor blackColor]];
  75. [showImageView setUserInteractionEnabled:NO];
  76. [showImageView setBackgroundColor:[UIColor blackColor]];
  77. glkshowImageView = [[GLKView alloc] initWithFrame:self.frame context:eagContext];
  78. [glkshowImageView setBackgroundColor:[UIColor blackColor]];
  79. [glkshowImageView bindDrawable];
  80. [glkshowImageView setUserInteractionEnabled:NO];
  81. [touchCommondView addSubview:showImageView];
  82. [showImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.left.mas_equalTo(0.f);
  84. make.right.mas_equalTo(0.f);
  85. make.bottom.mas_equalTo(0.f);
  86. make.top.mas_equalTo(0.f);
  87. }];
  88. [touchCommondView addSubview:glkshowImageView];
  89. [glkshowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  90. make.left.mas_equalTo(0.f);
  91. make.right.mas_equalTo(0.f);
  92. make.bottom.mas_equalTo(0.f);
  93. make.top.mas_equalTo(0.f);
  94. }];
  95. /*控制按钮*/
  96. UIImage *driftBtnImage = [UIImage imageNamed:@"you_icon"];
  97. controlBtn = [[UIButton alloc] init];
  98. [controlBtn setBackgroundColor:[UIColor clearColor]];
  99. [controlBtn addTarget:self
  100. action:@selector(controlBtnPressed:)
  101. forControlEvents:(UIControlEventTouchUpInside)];
  102. UIPanGestureRecognizer *gester = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureCallback:)];
  103. [controlBtn addGestureRecognizer:gester];
  104. gester.delegate = self;
  105. gester = nil;
  106. [controlBtn setImage:driftBtnImage forState:(UIControlStateNormal)];
  107. [controlBtn addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(controlBtnLongPress:)]];
  108. /*下方控制器*/
  109. bottomContrView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 47.f, frame.size.width, 47.f)];
  110. [bottomContrView setBackgroundColor:[UIColor blackColor]];
  111. [self addSubview:bottomContrView];
  112. [bottomContrView mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.right.mas_equalTo(0);
  114. make.height.mas_equalTo(47.f);
  115. make.left.mas_equalTo(0.f);
  116. make.bottom.mas_equalTo(0.f);
  117. }];
  118. [bottomContrView setHidden:YES];
  119. [self initAddSubViewForBottomContrView];
  120. [self setPoMas_makeWithImageRate:frame.size.width/frame.size.height isOpenVirtual:NO isUsageMode:NO];
  121. }
  122. - (void)initAddSubViewForBottomContrView{
  123. /*第一个app菜单*/
  124. UIImage *mueBtnImage = [UIImage imageNamed:@"houtai_icon"];
  125. mueBtn = [[UIButton alloc] init];
  126. [mueBtn setBackgroundColor:[UIColor clearColor]];
  127. [mueBtn addTarget:self
  128. action:@selector(mueBtnPressed:)
  129. forControlEvents:(UIControlEventTouchUpInside)];
  130. [mueBtn setImage:mueBtnImage forState:(UIControlStateNormal)];
  131. [mueBtn setImageEdgeInsets:(UIEdgeInsetsMake(6.f, 6.f, 6.f, 6.f))];
  132. [bottomContrView addSubview:mueBtn];
  133. /*第二个home键*/
  134. UIImage *homeBtnImage = [UIImage imageNamed:@"home_icon"];
  135. homeBtn = [[UIButton alloc] init];
  136. [homeBtn setBackgroundColor:[UIColor clearColor]];
  137. [homeBtn addTarget:self
  138. action:@selector(homeBtnPressed:)
  139. forControlEvents:(UIControlEventTouchUpInside)];
  140. [homeBtn setImage:homeBtnImage forState:(UIControlStateNormal)];
  141. [homeBtn setImageEdgeInsets:(UIEdgeInsetsMake(6.f, 6.f, 6.f, 6.f))];
  142. [bottomContrView addSubview:homeBtn];
  143. /*第三个返回键*/
  144. UIImage *backBtnImage = [UIImage imageNamed:@"fanhui_you_icon"];
  145. backBtn = [[UIButton alloc] init];
  146. [backBtn setBackgroundColor:[UIColor clearColor]];
  147. [backBtn addTarget:self
  148. action:@selector(backBtnPressed:)
  149. forControlEvents:(UIControlEventTouchUpInside)];
  150. [backBtn setImage:backBtnImage forState:(UIControlStateNormal)];
  151. [backBtn setImageEdgeInsets:(UIEdgeInsetsMake(6.f, 6.f, 6.f, 6.f))];
  152. [bottomContrView addSubview:backBtn];
  153. //测试说图片方向反了
  154. backBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI);
  155. }
  156. - (void)mueBtnPressed:(id)sender{
  157. if ([delegate respondsToSelector:@selector(mueBtnBePressed:)]) {
  158. [delegate mueBtnBePressed:sender];
  159. }
  160. }
  161. - (void)homeBtnPressed:(id)sender{
  162. if ([delegate respondsToSelector:@selector(homeBtnBePressed:)]) {
  163. [delegate homeBtnBePressed:sender];
  164. }
  165. }
  166. - (void)backBtnPressed:(id)sender{
  167. if ([delegate respondsToSelector:@selector(backBtnBePressed:)]) {
  168. [delegate backBtnBePressed:sender];
  169. }
  170. }
  171. - (void)initPoMas_makeWithImageRate:(BOOL)isOpenVirtual isUsageMode:(BOOL)isUsageMode{
  172. [self setPoMas_makeWithImageRate:self.frame.size.width/self.frame.size.height isOpenVirtual:isOpenVirtual isUsageMode:isUsageMode];
  173. }
  174. - (void)setPoMas_makeWithImageRate:(CGFloat)w_h_rate isOpenVirtual:(BOOL)isOpenVirtual isUsageMode:(BOOL)isUsageMode{
  175. /*播放视图*/
  176. // touchCommondView.isLan = NO;
  177. [showImageView setBackgroundColor:[UIColor blackColor]];
  178. [touchCommondView mas_remakeConstraints:^(MASConstraintMaker *make) {
  179. CGFloat scr_W = SCREEN_W;
  180. CGFloat scr_H = SCREEN_H;
  181. if (SCREEN_W > SCREEN_H){
  182. scr_W = SCREEN_H;
  183. scr_H = SCREEN_W;
  184. }
  185. if ((scr_H - scr_W/w_h_rate - 40.f)/2.f < 0){
  186. make.height.mas_equalTo(scr_H - 40.f);
  187. make.width.mas_equalTo((scr_H - 40.f)*w_h_rate);
  188. make.centerX.equalTo(self.mas_centerX);
  189. make.top.mas_equalTo(0);
  190. }
  191. else{
  192. make.left.mas_equalTo(0.f);
  193. make.height.mas_equalTo(scr_W/w_h_rate);
  194. make.right.mas_equalTo(0.f);
  195. make.top.mas_equalTo((scr_H - scr_W/w_h_rate - 40.f)/2.f);
  196. }
  197. }];
  198. [showImageView setTransform:CGAffineTransformMakeRotation(0)];
  199. [glkshowImageView setTransform:CGAffineTransformMakeRotation(0)];
  200. [showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  201. make.left.equalTo(touchCommondView.mas_left);
  202. make.right.equalTo(touchCommondView.mas_right);
  203. make.top.equalTo(touchCommondView.mas_top);
  204. make.bottom.equalTo(touchCommondView.mas_bottom);
  205. }];
  206. [self.glkshowImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  207. make.left.equalTo(touchCommondView.mas_left);
  208. make.right.equalTo(touchCommondView.mas_right);
  209. make.top.equalTo(touchCommondView.mas_top);
  210. make.bottom.equalTo(touchCommondView.mas_bottom);
  211. }];
  212. [bottomContrView setHidden:NO];
  213. [bottomContrView mas_remakeConstraints:^(MASConstraintMaker *make) {
  214. make.height.mas_equalTo(40.f);
  215. make.right.equalTo(touchCommondView.mas_right);
  216. make.top.equalTo(touchCommondView.mas_bottom);
  217. make.left.equalTo(touchCommondView.mas_left);
  218. }];
  219. [mueBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  220. make.left.mas_equalTo(41.f);
  221. make.width.and.height.mas_equalTo(40.f);
  222. make.top.mas_equalTo(0);
  223. }];
  224. [homeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  225. make.centerX.equalTo(bottomContrView.mas_centerX);
  226. make.width.and.height.mas_equalTo(40.f);
  227. make.top.mas_equalTo(0);
  228. }];
  229. UIImage *backBtnImage = [UIImage imageNamed:@"fanhui_you_icon"];
  230. // UIImage *backBtnImage = [UIImage imageNamed:@"fanhui_icon"];
  231. [backBtn setImage:backBtnImage forState:(UIControlStateNormal)];
  232. [backBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  233. make.right.mas_equalTo(-41.f);
  234. make.width.and.height.mas_equalTo(40.f);
  235. make.top.mas_equalTo(0);
  236. }];
  237. }
  238. /**
  239. * @brief 设置横屏模式UI布局
  240. *
  241. * @param w_h_rate 宽高比
  242. *
  243. * @param isOpenVirtual 是否有打开底部虚拟按键
  244. *
  245. * @param isUsageMode 是否为专业模式
  246. *
  247. */
  248. - (void)setLanMas_makeWithImageRate:(CGFloat)w_h_rate isOpenVirtual:(BOOL)isOpenVirtual isUsageMode:(BOOL)isUsageMode{
  249. /*需要考虑专业模式*/
  250. /*暂时只考虑非专业模式无虚拟键布局*/
  251. CGFloat h_forPhoneTop = 0;
  252. if (IPHONE_X){
  253. h_forPhoneTop = 44.f;
  254. }
  255. CGFloat des_W = SCREEN_W<SCREEN_H?SCREEN_W:SCREEN_H;/*实际较小的数字*/
  256. CGFloat des_H = (SCREEN_W>SCREEN_H?SCREEN_W:SCREEN_H) - h_forPhoneTop;/*实际较大的数字*/
  257. // touchCommondView.isLan = YES;
  258. [touchCommondView mas_remakeConstraints:^(MASConstraintMaker *make) {
  259. make.bottom.mas_equalTo(0.f);
  260. make.width.mas_equalTo(des_W*w_h_rate);
  261. make.left.mas_equalTo(h_forPhoneTop + (des_H - des_W*w_h_rate)/2.f);
  262. make.top.mas_equalTo(0);
  263. }];
  264. CGFloat des_H1 = des_W*w_h_rate;/*实际较大的数字*/
  265. CGFloat des_W1 = des_W;/*实际较小的数字*/
  266. if (isUsageMode){
  267. des_W1 = des_W-44.f;
  268. des_H1 = (des_W-44.f)*w_h_rate;
  269. }
  270. [showImageView setTransform:CGAffineTransformMakeRotation(-M_PI/2)];
  271. [glkshowImageView setTransform:CGAffineTransformMakeRotation(-M_PI/2)];
  272. [showImageView setBackgroundColor:[UIColor blackColor]];
  273. [showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  274. make.height.mas_equalTo(des_H1);
  275. make.width.mas_equalTo((des_W1));
  276. make.left.mas_equalTo((des_H1 - des_W1)/2.f);
  277. make.top.mas_equalTo((des_W1 - des_H1)/2.f);
  278. }];
  279. [self.glkshowImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  280. /*动态处理宽高*/
  281. make.height.mas_equalTo(des_H1);
  282. make.width.mas_equalTo((des_W1));
  283. make.left.mas_equalTo((des_H1 - des_W1)/2.f);
  284. make.top.mas_equalTo((des_W1 - des_H1)/2.f);
  285. }];
  286. }
  287. - (void)controlBtnPressed:(id)sender{
  288. if ([delegate respondsToSelector:@selector(useInfoBtnBePressed:)]) {
  289. [delegate useInfoBtnBePressed:sender];
  290. }
  291. }
  292. static int sendCount = 0;
  293. #pragma -mark showImageViewDelegate
  294. - (void)showImageViewtouchesBeganCommondStr:(NSString *)commondStr{
  295. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  296. sendCount = 1;
  297. [delegate playerShowImageViewtouchesStr:commondStr];
  298. HLog(@"\n-----showImageViewtouchesBeganCommondStr-----");
  299. }
  300. }
  301. - (void)showImageViewtouchesEndedCommondStr:(NSString *)commondStr{
  302. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  303. HLog(@"\n-----showImageViewtouchesEndedCommondStr-----");
  304. sendCount = 0;
  305. [delegate playerShowImageViewtouchesStr:commondStr];
  306. }
  307. }
  308. - (void)showImageViewtouchesMovedCommondStr:(NSString *)commondStr{
  309. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  310. HLog(@"\n-----showImageViewtouchesMovedCommondStr-----");
  311. if (sendCount%2 == 0){
  312. [delegate playerShowImageViewtouchesStr:commondStr];
  313. }
  314. sendCount++;
  315. }
  316. }
  317. - (void)showImageViewtouchesCancelledCommondStr:(NSString *)commondStr{
  318. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  319. HLog(@"\n-----showImageViewtouchesCancelledCommondStr-----");
  320. sendCount = 0;
  321. [delegate playerShowImageViewtouchesStr:commondStr];
  322. }
  323. }
  324. - (void)controlBtnLongPress:(UILongPressGestureRecognizer *)longPressGesture {
  325. switch (longPressGesture.state) {
  326. case UIGestureRecognizerStateBegan:{
  327. // HLog(@"云手机推流界面 控制按钮长按手势开始");
  328. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchBeganNotification object:nil userInfo:nil];
  329. break;
  330. case UIGestureRecognizerStateEnded:{
  331. // HLog(@"云手机推流界面 控制按钮长按手势结束");
  332. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchEndNotification object:nil userInfo:nil];
  333. break;
  334. }
  335. default:
  336. break;
  337. }
  338. }
  339. }
  340. static CGRect startFrame;
  341. - (void)panGestureCallback:(UIPanGestureRecognizer *)panGesture{
  342. self.controlBtn.alpha = 1;
  343. switch (panGesture.state) {
  344. case UIGestureRecognizerStateBegan:{
  345. startFrame = controlBtn.frame;
  346. // HLog(@"云手机推流界面 控制按钮拖动手势开始");
  347. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchBeganNotification object:nil userInfo:nil];
  348. break;
  349. }
  350. case UIGestureRecognizerStateChanged:{
  351. CGPoint translatedPoint = [panGesture translationInView:self];/*KXLY*/
  352. CGRect frame = controlBtn.frame;
  353. frame.origin.x = translatedPoint.x + startFrame.origin.x;
  354. frame.origin.y = translatedPoint.y + startFrame.origin.y;
  355. controlBtn.frame = frame;
  356. // HLog(@"\n--(%f,%f)--controlBtn.frame--X-%f",translatedPoint.x,translatedPoint.x,controlBtn.frame.origin.x);
  357. break;
  358. }
  359. case UIGestureRecognizerStateEnded:
  360. // HLog(@"云手机推流界面 控制按钮拖动手势结束");
  361. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchEndNotification object:nil userInfo:nil];
  362. /*防止控制器移出屏幕*/
  363. CGRect frame = controlBtn.frame;
  364. if (frame.origin.x < 10.f/2.f){
  365. frame.origin.x = 10.f/2.f;
  366. }
  367. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  368. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  369. }
  370. if (frame.size.width + frame.origin.x + 10.f/2.f > self.width){
  371. frame.origin.x = self.width - 10.f/2.f - frame.size.width;
  372. }
  373. if (frame.size.height + frame.origin.y + 10.f/2.f > self.height){
  374. frame.origin.y = self.height - 10.f/2.f - frame.size.height;
  375. }
  376. controlBtn.frame = frame;
  377. break;
  378. case UIGestureRecognizerStateCancelled: {
  379. /*防止控制器移出屏幕*/
  380. CGRect frame = controlBtn.frame;
  381. if (frame.origin.x < 10.f/2.f){
  382. frame.origin.x = 10.f/2.f;
  383. }
  384. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  385. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  386. }
  387. if (frame.size.width + frame.origin.x + 10.f/2.f > self.width){
  388. frame.origin.x = self.width - 10.f/2.f - frame.size.width;
  389. }
  390. if (frame.size.height + frame.origin.y + 10.f/2.f > self.height){
  391. frame.origin.y = self.height - 10.f/2.f - frame.size.height;
  392. }
  393. controlBtn.frame = frame;
  394. break;
  395. }
  396. default:
  397. break;
  398. }
  399. }
  400. - (void)setIsLan:(BOOL)isLan{
  401. self->isLan = isLan;
  402. /*设置视图方向*/
  403. showImageView.isLan = isLan;
  404. touchCommondView.isLan = isLan;
  405. }
  406. @end