PlayerView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 setBackgroundImage:driftBtnImage forState:(UIControlStateNormal)];
  108. [controlBtn addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(controlBtnLongPress:)]];
  109. /*下方控制器*/
  110. bottomContrView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 47.f, frame.size.width, 47.f)];
  111. [bottomContrView setBackgroundColor:[UIColor blackColor]];
  112. [self addSubview:bottomContrView];
  113. [bottomContrView mas_makeConstraints:^(MASConstraintMaker *make) {
  114. make.right.mas_equalTo(0);
  115. make.height.mas_equalTo(47.f);
  116. make.left.mas_equalTo(0.f);
  117. make.bottom.mas_equalTo(0.f);
  118. }];
  119. [bottomContrView setHidden:YES];
  120. [self initAddSubViewForBottomContrView];
  121. [self setPoMas_makeWithImageRate:frame.size.width/frame.size.height isOpenVirtual:NO isUsageMode:NO];
  122. }
  123. - (void)initAddSubViewForBottomContrView{
  124. /*第一个app菜单*/
  125. UIImage *mueBtnImage = [UIImage imageNamed:@"houtai_icon"];
  126. mueBtn = [[UIButton alloc] init];
  127. [mueBtn setBackgroundColor:[UIColor clearColor]];
  128. [mueBtn addTarget:self
  129. action:@selector(mueBtnPressed:)
  130. forControlEvents:(UIControlEventTouchUpInside)];
  131. [mueBtn setImage:mueBtnImage forState:(UIControlStateNormal)];
  132. [mueBtn setImageEdgeInsets:(UIEdgeInsetsMake(6.f, 6.f, 6.f, 6.f))];
  133. [bottomContrView addSubview:mueBtn];
  134. /*第二个home键*/
  135. UIImage *homeBtnImage = [UIImage imageNamed:@"home_icon"];
  136. homeBtn = [[UIButton alloc] init];
  137. [homeBtn setBackgroundColor:[UIColor clearColor]];
  138. [homeBtn addTarget:self
  139. action:@selector(homeBtnPressed:)
  140. forControlEvents:(UIControlEventTouchUpInside)];
  141. [homeBtn setImage:homeBtnImage forState:(UIControlStateNormal)];
  142. [homeBtn setImageEdgeInsets:(UIEdgeInsetsMake(6.f, 6.f, 6.f, 6.f))];
  143. [bottomContrView addSubview:homeBtn];
  144. /*第三个返回键*/
  145. UIImage *backBtnImage = [UIImage imageNamed:@"fanhui_you_icon"];
  146. backBtn = [[UIButton alloc] init];
  147. [backBtn setBackgroundColor:[UIColor clearColor]];
  148. [backBtn addTarget:self
  149. action:@selector(backBtnPressed:)
  150. forControlEvents:(UIControlEventTouchUpInside)];
  151. [backBtn setImage:backBtnImage forState:(UIControlStateNormal)];
  152. [backBtn setImageEdgeInsets:(UIEdgeInsetsMake(6.f, 6.f, 6.f, 6.f))];
  153. [bottomContrView addSubview:backBtn];
  154. //测试说图片方向反了
  155. backBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI);
  156. }
  157. - (void)mueBtnPressed:(id)sender{
  158. if ([delegate respondsToSelector:@selector(mueBtnBePressed:)]) {
  159. [delegate mueBtnBePressed:sender];
  160. }
  161. }
  162. - (void)homeBtnPressed:(id)sender{
  163. if ([delegate respondsToSelector:@selector(homeBtnBePressed:)]) {
  164. [delegate homeBtnBePressed:sender];
  165. }
  166. }
  167. - (void)backBtnPressed:(id)sender{
  168. if ([delegate respondsToSelector:@selector(backBtnBePressed:)]) {
  169. [delegate backBtnBePressed:sender];
  170. }
  171. }
  172. - (void)initPoMas_makeWithImageRate:(BOOL)isOpenVirtual isUsageMode:(BOOL)isUsageMode{
  173. [self setPoMas_makeWithImageRate:self.frame.size.width/self.frame.size.height isOpenVirtual:isOpenVirtual isUsageMode:isUsageMode];
  174. }
  175. - (void)setPoMas_makeWithImageRate:(CGFloat)w_h_rate isOpenVirtual:(BOOL)isOpenVirtual isUsageMode:(BOOL)isUsageMode{
  176. /*播放视图*/
  177. // touchCommondView.isLan = NO;
  178. [showImageView setBackgroundColor:[UIColor blackColor]];
  179. [touchCommondView mas_remakeConstraints:^(MASConstraintMaker *make) {
  180. CGFloat scr_W = SCREEN_W;
  181. CGFloat scr_H = SCREEN_H;
  182. if (SCREEN_W > SCREEN_H){
  183. scr_W = SCREEN_H;
  184. scr_H = SCREEN_W;
  185. }
  186. if ((scr_H - scr_W/w_h_rate - 40.f)/2.f < 0){
  187. make.height.mas_equalTo(scr_H - 40.f);
  188. make.width.mas_equalTo((scr_H - 40.f)*w_h_rate);
  189. make.centerX.equalTo(self.mas_centerX);
  190. make.top.mas_equalTo(0);
  191. }
  192. else{
  193. make.left.mas_equalTo(0.f);
  194. make.height.mas_equalTo(scr_W/w_h_rate);
  195. make.right.mas_equalTo(0.f);
  196. make.top.mas_equalTo((scr_H - scr_W/w_h_rate - 40.f)/2.f);
  197. }
  198. }];
  199. [showImageView setTransform:CGAffineTransformMakeRotation(0)];
  200. [glkshowImageView setTransform:CGAffineTransformMakeRotation(0)];
  201. [showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  202. make.left.equalTo(touchCommondView.mas_left);
  203. make.right.equalTo(touchCommondView.mas_right);
  204. make.top.equalTo(touchCommondView.mas_top);
  205. make.bottom.equalTo(touchCommondView.mas_bottom);
  206. }];
  207. [self.glkshowImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  208. make.left.equalTo(touchCommondView.mas_left);
  209. make.right.equalTo(touchCommondView.mas_right);
  210. make.top.equalTo(touchCommondView.mas_top);
  211. make.bottom.equalTo(touchCommondView.mas_bottom);
  212. }];
  213. [bottomContrView setHidden:NO];
  214. [bottomContrView mas_remakeConstraints:^(MASConstraintMaker *make) {
  215. make.height.mas_equalTo(40.f);
  216. make.right.equalTo(touchCommondView.mas_right);
  217. make.top.equalTo(touchCommondView.mas_bottom);
  218. make.left.equalTo(touchCommondView.mas_left);
  219. }];
  220. [mueBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  221. make.left.mas_equalTo(41.f);
  222. make.width.and.height.mas_equalTo(40.f);
  223. make.top.mas_equalTo(0);
  224. }];
  225. [homeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  226. make.centerX.equalTo(bottomContrView.mas_centerX);
  227. make.width.and.height.mas_equalTo(40.f);
  228. make.top.mas_equalTo(0);
  229. }];
  230. UIImage *backBtnImage = [UIImage imageNamed:@"fanhui_you_icon"];
  231. // UIImage *backBtnImage = [UIImage imageNamed:@"fanhui_icon"];
  232. [backBtn setImage:backBtnImage forState:(UIControlStateNormal)];
  233. [backBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  234. make.right.mas_equalTo(-41.f);
  235. make.width.and.height.mas_equalTo(40.f);
  236. make.top.mas_equalTo(0);
  237. }];
  238. }
  239. /**
  240. * @brief 设置横屏模式UI布局
  241. *
  242. * @param w_h_rate 宽高比
  243. *
  244. * @param isOpenVirtual 是否有打开底部虚拟按键
  245. *
  246. * @param isUsageMode 是否为专业模式
  247. *
  248. */
  249. - (void)setLanMas_makeWithImageRate:(CGFloat)w_h_rate isOpenVirtual:(BOOL)isOpenVirtual isUsageMode:(BOOL)isUsageMode{
  250. /*需要考虑专业模式*/
  251. /*暂时只考虑非专业模式无虚拟键布局*/
  252. CGFloat h_forPhoneTop = 0;
  253. if (IPHONE_X){
  254. h_forPhoneTop = 44.f;
  255. }
  256. CGFloat des_W = SCREEN_W<SCREEN_H?SCREEN_W:SCREEN_H;/*实际较小的数字*/
  257. CGFloat des_H = (SCREEN_W>SCREEN_H?SCREEN_W:SCREEN_H) - h_forPhoneTop;/*实际较大的数字*/
  258. // touchCommondView.isLan = YES;
  259. [touchCommondView mas_remakeConstraints:^(MASConstraintMaker *make) {
  260. make.bottom.mas_equalTo(0.f);
  261. make.width.mas_equalTo(des_W*w_h_rate);
  262. make.left.mas_equalTo(h_forPhoneTop + (des_H - des_W*w_h_rate)/2.f);
  263. make.top.mas_equalTo(0);
  264. }];
  265. CGFloat des_H1 = des_W*w_h_rate;/*实际较大的数字*/
  266. CGFloat des_W1 = des_W;/*实际较小的数字*/
  267. if (isUsageMode){
  268. des_W1 = des_W-44.f;
  269. des_H1 = (des_W-44.f)*w_h_rate;
  270. }
  271. [showImageView setTransform:CGAffineTransformMakeRotation(-M_PI/2)];
  272. [glkshowImageView setTransform:CGAffineTransformMakeRotation(-M_PI/2)];
  273. [showImageView setBackgroundColor:[UIColor blackColor]];
  274. [showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  275. make.height.mas_equalTo(des_H1);
  276. make.width.mas_equalTo((des_W1));
  277. make.left.mas_equalTo((des_H1 - des_W1)/2.f);
  278. make.top.mas_equalTo((des_W1 - des_H1)/2.f);
  279. }];
  280. [self.glkshowImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  281. /*动态处理宽高*/
  282. make.height.mas_equalTo(des_H1);
  283. make.width.mas_equalTo((des_W1));
  284. make.left.mas_equalTo((des_H1 - des_W1)/2.f);
  285. make.top.mas_equalTo((des_W1 - des_H1)/2.f);
  286. }];
  287. }
  288. - (void)controlBtnPressed:(id)sender{
  289. if ([delegate respondsToSelector:@selector(useInfoBtnBePressed:)]) {
  290. [delegate useInfoBtnBePressed:sender];
  291. }
  292. }
  293. static int sendCount = 0;
  294. #pragma -mark showImageViewDelegate
  295. - (void)showImageViewtouchesBeganCommondStr:(NSString *)commondStr{
  296. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  297. sendCount = 1;
  298. [delegate playerShowImageViewtouchesStr:commondStr];
  299. HLog(@"\n-----showImageViewtouchesBeganCommondStr-----");
  300. }
  301. }
  302. - (void)showImageViewtouchesEndedCommondStr:(NSString *)commondStr{
  303. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  304. HLog(@"\n-----showImageViewtouchesEndedCommondStr-----");
  305. sendCount = 0;
  306. [delegate playerShowImageViewtouchesStr:commondStr];
  307. }
  308. }
  309. - (void)showImageViewtouchesMovedCommondStr:(NSString *)commondStr{
  310. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  311. HLog(@"\n-----showImageViewtouchesMovedCommondStr-----");
  312. if (sendCount%2 == 0){
  313. [delegate playerShowImageViewtouchesStr:commondStr];
  314. }
  315. sendCount++;
  316. }
  317. }
  318. - (void)showImageViewtouchesCancelledCommondStr:(NSString *)commondStr{
  319. if ([delegate respondsToSelector:@selector(playerShowImageViewtouchesStr:)]){
  320. HLog(@"\n-----showImageViewtouchesCancelledCommondStr-----");
  321. sendCount = 0;
  322. [delegate playerShowImageViewtouchesStr:commondStr];
  323. }
  324. }
  325. - (void)controlBtnLongPress:(UILongPressGestureRecognizer *)longPressGesture {
  326. switch (longPressGesture.state) {
  327. case UIGestureRecognizerStateBegan:{
  328. // HLog(@"云手机推流界面 控制按钮长按手势开始");
  329. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchBeganNotification object:nil userInfo:nil];
  330. break;
  331. case UIGestureRecognizerStateEnded:{
  332. // HLog(@"云手机推流界面 控制按钮长按手势结束");
  333. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchEndNotification object:nil userInfo:nil];
  334. break;
  335. }
  336. default:
  337. break;
  338. }
  339. }
  340. }
  341. static CGRect startFrame;
  342. - (void)panGestureCallback:(UIPanGestureRecognizer *)panGesture{
  343. self.controlBtn.alpha = 1;
  344. switch (panGesture.state) {
  345. case UIGestureRecognizerStateBegan:{
  346. startFrame = controlBtn.frame;
  347. // HLog(@"云手机推流界面 控制按钮拖动手势开始");
  348. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchBeganNotification object:nil userInfo:nil];
  349. break;
  350. }
  351. case UIGestureRecognizerStateChanged:{
  352. CGPoint translatedPoint = [panGesture translationInView:self];/*KXLY*/
  353. CGRect frame = controlBtn.frame;
  354. frame.origin.x = translatedPoint.x + startFrame.origin.x;
  355. frame.origin.y = translatedPoint.y + startFrame.origin.y;
  356. controlBtn.frame = frame;
  357. // HLog(@"\n--(%f,%f)--controlBtn.frame--X-%f",translatedPoint.x,translatedPoint.x,controlBtn.frame.origin.x);
  358. break;
  359. }
  360. case UIGestureRecognizerStateEnded:
  361. // HLog(@"云手机推流界面 控制按钮拖动手势结束");
  362. [[NSNotificationCenter defaultCenter] postNotificationName:PlayerViewControlBtnTouchEndNotification object:nil userInfo:nil];
  363. /*防止控制器移出屏幕*/
  364. CGRect frame = controlBtn.frame;
  365. if (frame.origin.x < 10.f/2.f){
  366. frame.origin.x = 10.f/2.f;
  367. }
  368. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  369. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  370. }
  371. if (frame.size.width + frame.origin.x + 10.f/2.f > self.width){
  372. frame.origin.x = self.width - 10.f/2.f - frame.size.width;
  373. }
  374. if (frame.size.height + frame.origin.y + 10.f/2.f > self.height){
  375. frame.origin.y = self.height - 10.f/2.f - frame.size.height;
  376. }
  377. controlBtn.frame = frame;
  378. break;
  379. case UIGestureRecognizerStateCancelled: {
  380. /*防止控制器移出屏幕*/
  381. CGRect frame = controlBtn.frame;
  382. if (frame.origin.x < 10.f/2.f){
  383. frame.origin.x = 10.f/2.f;
  384. }
  385. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  386. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  387. }
  388. if (frame.size.width + frame.origin.x + 10.f/2.f > self.width){
  389. frame.origin.x = self.width - 10.f/2.f - frame.size.width;
  390. }
  391. if (frame.size.height + frame.origin.y + 10.f/2.f > self.height){
  392. frame.origin.y = self.height - 10.f/2.f - frame.size.height;
  393. }
  394. controlBtn.frame = frame;
  395. break;
  396. }
  397. default:
  398. break;
  399. }
  400. }
  401. - (void)setIsLan:(BOOL)isLan{
  402. self->isLan = isLan;
  403. /*设置视图方向*/
  404. showImageView.isLan = isLan;
  405. touchCommondView.isLan = isLan;
  406. }
  407. @end