@@ -78,6 +78,9 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
7878
7979@interface CCEAGLView ()
8080@property (nonatomic ) CCInputView* textInputView;
81+ @property (nonatomic ) BOOL isKeyboardShown;
82+ @property (nonatomic , copy ) NSNotification * keyboardShowNotification;
83+ @property (nonatomic , assign ) CGRect originalRect;
8184@end
8285
8386@implementation CCEAGLView
@@ -86,6 +89,9 @@ @implementation CCEAGLView
8689@synthesize pixelFormat=pixelformat_, depthFormat=depthFormat_;
8790@synthesize context=context_;
8891@synthesize multiSampling=multiSampling_;
92+ @synthesize keyboardShowNotification = keyboardShowNotification_;
93+ @synthesize isKeyboardShown;
94+ @synthesize originalRect = originalRect_;
8995
9096+ (Class ) layerClass
9197{
@@ -137,6 +143,8 @@ - (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GL
137143 return nil ;
138144 }
139145
146+ originalRect_ = self.frame ;
147+ self.keyboardShowNotification = nil ;
140148 if ([self respondsToSelector: @selector (setContentScaleFactor: )])
141149 {
142150 self.contentScaleFactor = [[UIScreen mainScreen ] scale ];
@@ -466,16 +474,206 @@ - (void) hideKeyboard
466474
467475-(BOOL ) isKeyboardShown
468476{
469- return [ self .textInputView isKeyboardShown ] ;
477+ return self.isKeyboardShown ;
470478}
471479
472480-(void ) doAnimationWhenKeyboardMoveWithDuration : (float ) duration distance : (float ) dis
473481{
474-
482+ [UIView beginAnimations: nil context: nullptr ];
483+ [UIView setAnimationDelegate: self ];
484+ [UIView setAnimationDuration: duration];
485+ [UIView setAnimationBeginsFromCurrentState: YES ];
486+
487+ // NSLog(@"[animation] dis = %f, scale = %f \n", dis, cocos2d::GLView::getInstance()->getScaleY());
488+
489+ if (dis < 0 .0f ) dis = 0 .0f ;
490+
491+ auto glview = cocos2d::Director::getInstance ()->getOpenGLView ();
492+ dis *= glview->getScaleY ();
493+
494+ dis /= self.contentScaleFactor ;
495+
496+ #if defined(CC_TARGET_OS_TVOS)
497+ self.frame = CGRectMake (originalRect_.origin .x , originalRect_.origin .y - dis, originalRect_.size .width , originalRect_.size .height );
498+ #else
499+ switch (getFixedOrientation ([[UIApplication sharedApplication ] statusBarOrientation ]))
500+ {
501+ case UIInterfaceOrientationPortrait:
502+ self.frame = CGRectMake (originalRect_.origin .x , originalRect_.origin .y - dis, originalRect_.size .width , originalRect_.size .height );
503+ break ;
504+
505+ case UIInterfaceOrientationPortraitUpsideDown:
506+ self.frame = CGRectMake (originalRect_.origin .x , originalRect_.origin .y + dis, originalRect_.size .width , originalRect_.size .height );
507+ break ;
508+
509+ case UIInterfaceOrientationLandscapeLeft:
510+ self.frame = CGRectMake (originalRect_.origin .x - dis, originalRect_.origin .y , originalRect_.size .width , originalRect_.size .height );
511+ break ;
512+
513+ case UIInterfaceOrientationLandscapeRight:
514+ self.frame = CGRectMake (originalRect_.origin .x + dis, originalRect_.origin .y , originalRect_.size .width , originalRect_.size .height );
515+ break ;
516+
517+ default :
518+ break ;
519+ }
520+ #endif
521+
522+ [UIView commitAnimations ];
475523}
476524
477525-(void ) doAnimationWhenAnotherEditBeClicked
478526{
527+ if (self.keyboardShowNotification != nil )
528+ {
529+ [[NSNotificationCenter defaultCenter ]postNotification:self .keyboardShowNotification];
530+ }
531+ }
532+
533+ #pragma UIKeyboard notification
534+
535+ #if !defined(CC_TARGET_OS_TVOS)
536+ namespace {
537+ UIInterfaceOrientation getFixedOrientation (UIInterfaceOrientation statusBarOrientation)
538+ {
539+ if ([[[UIDevice currentDevice ] systemVersion ] floatValue ] >= 8.0 )
540+ {
541+ statusBarOrientation = UIInterfaceOrientationPortrait;
542+ }
543+ return statusBarOrientation;
544+ }
545+ }
546+ #endif
547+
548+ - (void )didMoveToWindow
549+ {
550+ #if !defined(CC_TARGET_OS_TVOS)
551+ [[NSNotificationCenter defaultCenter ] addObserver: self
552+ selector: @selector (onUIKeyboardNotification: )
553+ name: UIKeyboardWillShowNotification object: nil ];
554+
555+ [[NSNotificationCenter defaultCenter ] addObserver: self
556+ selector: @selector (onUIKeyboardNotification: )
557+ name: UIKeyboardDidShowNotification object: nil ];
558+ [[NSNotificationCenter defaultCenter ] addObserver: self
559+ selector: @selector (onUIKeyboardNotification: )
560+ name: UIKeyboardWillHideNotification object: nil ];
561+
562+ [[NSNotificationCenter defaultCenter ] addObserver: self
563+ selector: @selector (onUIKeyboardNotification: )
564+ name: UIKeyboardDidHideNotification object: nil ];
565+ #endif
566+ }
567+
568+ - (void )onUIKeyboardNotification : (NSNotification *)notif
569+ {
570+ NSString * type = notif.name ;
571+
572+ NSDictionary * info = [notif userInfo ];
573+ CGRect begin = [self convertRect:
574+ [[info objectForKey: UIKeyboardFrameBeginUserInfoKey] CGRectValue ]
575+ fromView: self ];
576+ CGRect end = [self convertRect:
577+ [[info objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue ]
578+ fromView: self ];
579+ double aniDuration = [[info objectForKey: UIKeyboardAnimationDurationUserInfoKey] doubleValue ];
580+
581+ CGSize viewSize = self.frame .size ;
582+
583+ CGFloat tmp;
584+ switch (getFixedOrientation ([[UIApplication sharedApplication ] statusBarOrientation ]))
585+ {
586+ case UIInterfaceOrientationPortrait:
587+ begin.origin .y = viewSize.height - begin.origin .y - begin.size .height ;
588+ end.origin .y = viewSize.height - end.origin .y - end.size .height ;
589+ break ;
590+
591+ case UIInterfaceOrientationPortraitUpsideDown:
592+ begin.origin .x = viewSize.width - (begin.origin .x + begin.size .width );
593+ end.origin .x = viewSize.width - (end.origin .x + end.size .width );
594+ break ;
595+
596+ case UIInterfaceOrientationLandscapeLeft:
597+ std::swap (begin.size .width , begin.size .height );
598+ std::swap (end.size .width , end.size .height );
599+ std::swap (viewSize.width , viewSize.height );
600+
601+ tmp = begin.origin .x ;
602+ begin.origin .x = begin.origin .y ;
603+ begin.origin .y = viewSize.height - tmp - begin.size .height ;
604+ tmp = end.origin .x ;
605+ end.origin .x = end.origin .y ;
606+ end.origin .y = viewSize.height - tmp - end.size .height ;
607+ break ;
608+
609+ case UIInterfaceOrientationLandscapeRight:
610+ std::swap (begin.size .width , begin.size .height );
611+ std::swap (end.size .width , end.size .height );
612+ std::swap (viewSize.width , viewSize.height );
613+
614+ tmp = begin.origin .x ;
615+ begin.origin .x = begin.origin .y ;
616+ begin.origin .y = tmp;
617+ tmp = end.origin .x ;
618+ end.origin .x = end.origin .y ;
619+ end.origin .y = tmp;
620+ break ;
621+
622+ default :
623+ break ;
624+ }
625+
626+ auto glview = cocos2d::Director::getInstance ()->getOpenGLView ();
627+ float scaleX = glview->getScaleX ();
628+ float scaleY = glview->getScaleY ();
629+
630+ // Convert to pixel coordinate
631+ begin = CGRectApplyAffineTransform (begin, CGAffineTransformScale (CGAffineTransformIdentity, self.contentScaleFactor , self.contentScaleFactor ));
632+ end = CGRectApplyAffineTransform (end, CGAffineTransformScale (CGAffineTransformIdentity, self.contentScaleFactor , self.contentScaleFactor ));
633+
634+ float offestY = glview->getViewPortRect ().origin .y ;
635+ if (offestY < 0 .0f )
636+ {
637+ begin.origin .y += offestY;
638+ begin.size .height -= offestY;
639+ end.size .height -= offestY;
640+ }
641+
642+ // Convert to design coordinate
643+ begin = CGRectApplyAffineTransform (begin, CGAffineTransformScale (CGAffineTransformIdentity, 1 .0f /scaleX, 1 .0f /scaleY));
644+ end = CGRectApplyAffineTransform (end, CGAffineTransformScale (CGAffineTransformIdentity, 1 .0f /scaleX, 1 .0f /scaleY));
645+
646+
647+ cocos2d::IMEKeyboardNotificationInfo notiInfo;
648+ notiInfo.begin = cocos2d::Rect (begin.origin .x ,
649+ begin.origin .y ,
650+ begin.size .width ,
651+ begin.size .height );
652+ notiInfo.end = cocos2d::Rect (end.origin .x ,
653+ end.origin .y ,
654+ end.size .width ,
655+ end.size .height );
656+ notiInfo.duration = (float )aniDuration;
657+
658+ cocos2d::IMEDispatcher* dispatcher = cocos2d::IMEDispatcher::sharedDispatcher ();
659+ if (UIKeyboardWillShowNotification == type)
660+ {
661+ dispatcher->dispatchKeyboardWillShow (notiInfo);
662+ }
663+ else if (UIKeyboardDidShowNotification == type)
664+ {
665+ self.isKeyboardShown = YES ;
666+ dispatcher->dispatchKeyboardDidShow (notiInfo);
667+ }
668+ else if (UIKeyboardWillHideNotification == type)
669+ {
670+ dispatcher->dispatchKeyboardWillHide (notiInfo);
671+ }
672+ else if (UIKeyboardDidHideNotification == type)
673+ {
674+ self.isKeyboardShown = NO ;
675+ dispatcher->dispatchKeyboardDidHide (notiInfo);
676+ }
479677}
480678
481679@end
0 commit comments