cocos2d-x 2.x 中KeyboardMove在iOS8下异常 编辑框移动异常

前端之家收集整理的这篇文章主要介绍了cocos2d-x 2.x 中KeyboardMove在iOS8下异常 编辑框移动异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


在向上推送过程方向不对。

#pragma mark UIKeyboard notification

- (void)onUIKeyboardNotification:(NSNotification *)notif;
{
    NSString * type = notif.name;
    
    NSDictionary* info = [notif userInfo];
    CGRect begin = [self convertRect: 
                    [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]
                            fromView:self];
    CGRect end = [self convertRect: 
                  [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]
                          fromView:self];
    
    CGSize viewSize = self.frame.size;
    CGFloat tmp;
    float vision = [[[UIDevice currentDevice] systemVersion] floatValue];
    
    //    CCLOG("系统版本 = %f",vision);
    //
    if ( vision >= 8.0)
    {
        if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft
           ||[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
        {
            tmp = begin.size.width;
            begin.size.width = begin.size.height;
            begin.size.height = tmp;
            
            tmp = end.size.width;
            end.size.width = end.size.height;
            end.size.height = tmp;
            
            tmp = viewSize.width;
            viewSize.width = viewSize.height;
            viewSize.height = tmp;
            
            tmp = begin.origin.x;
            begin.origin.x = begin.origin.y;
            begin.origin.y = viewSize.height - tmp - begin.size.height;
            
            tmp = end.origin.x;
            end.origin.x = end.origin.y;
            end.origin.y = viewSize.height - tmp - end.size.height;
        }
    }
    
    double aniDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    
    
    switch ([[UIApplication sharedApplication] statusBarOrientation])
    {
        case UIInterfaceOrientationPortrait:
            begin.origin.y = viewSize.height - begin.origin.y - begin.size.height;
            end.origin.y = viewSize.height - end.origin.y - end.size.height;
            break;
            
        case UIInterfaceOrientationPortraitUpsideDown:
            begin.origin.x = viewSize.width - (begin.origin.x + begin.size.width);
            end.origin.x = viewSize.width - (end.origin.x + end.size.width);
            break;
            
        case UIInterfaceOrientationLandscapeLeft:
            tmp = begin.size.width;
            begin.size.width = begin.size.height;
            begin.size.height = tmp;
            tmp = end.size.width;
            end.size.width = end.size.height;
            end.size.height = tmp;
            tmp = viewSize.width;
            viewSize.width = viewSize.height;
            viewSize.height = tmp;
            
            tmp = begin.origin.x;
            begin.origin.x = begin.origin.y;
            begin.origin.y = viewSize.height - tmp - begin.size.height;
            tmp = end.origin.x;
            end.origin.x = end.origin.y;
            end.origin.y = viewSize.height - tmp - end.size.height;
            break;
            
        case UIInterfaceOrientationLandscapeRight:
            if ( vision < 8.0)
            {
                tmp = begin.size.width;
                begin.size.width = begin.size.height;
                begin.size.height = tmp;
                
                tmp = end.size.width;
                end.size.width = end.size.height;
                end.size.height = tmp;
                
                tmp = viewSize.width;
                viewSize.width = viewSize.height;
                viewSize.height = tmp;
                
                tmp = begin.origin.x;
                begin.origin.x = begin.origin.y;
                begin.origin.y = tmp;
                
                tmp = end.origin.x;
                end.origin.x = end.origin.y;
                end.origin.y = tmp;
            }
            else
            {
                tmp = begin.size.width;
                begin.size.width = begin.size.height;
                begin.size.height = tmp;
                
                tmp = end.size.width;
                end.size.width = end.size.height;
                end.size.height = tmp;
                
                tmp = viewSize.width;
                viewSize.width = viewSize.height;
                viewSize.height = tmp;
                
                tmp = begin.origin.x;
                begin.origin.x = begin.origin.y;
                begin.origin.y = viewSize.height - tmp - begin.size.height;
                
                tmp = end.origin.x;
                end.origin.x = end.origin.y;
                end.origin.y = viewSize.height - tmp - end.size.height;
            }
            break;
            
        default:
            break;
    }
    
    float scaleX = cocos2d::CCEGLView::sharedOpenGLView()->getScaleX();
	float scaleY = cocos2d::CCEGLView::sharedOpenGLView()->getScaleY();
    
    
    if (self.contentScaleFactor == 2.0f)
    {
        // Convert to pixel coordinate
        
        begin = CGRectApplyAffineTransform(begin,CGAffineTransformScale(CGAffineTransformIdentity,2.0f,2.0f));
        end = CGRectApplyAffineTransform(end,2.0f));
    }
    
    float offestY = cocos2d::CCEGLView::sharedOpenGLView()->getViewPortRect().origin.y;
    CCLOG("offestY = %f",offestY);
    if (offestY < 0.0f)
    {
        begin.origin.y += offestY;
        begin.size.height -= offestY;
        end.size.height -= offestY;
    }
    
    // Convert to desigin coordinate
    begin = CGRectApplyAffineTransform(begin,1.0f/scaleX,1.0f/scaleY));
    end = CGRectApplyAffineTransform(end,1.0f/scaleY));

    
    cocos2d::CCIMEKeyboardNotificationInfo notiInfo;
    notiInfo.begin = cocos2d::CCRect(begin.origin.x,begin.origin.y,begin.size.width,begin.size.height);
    notiInfo.end = cocos2d::CCRect(end.origin.x,end.origin.y,end.size.width,end.size.height);
    notiInfo.duration = (float)aniDuration;
    
    cocos2d::CCIMEDispatcher* dispatcher = cocos2d::CCIMEDispatcher::sharedDispatcher();
    if (UIKeyboardWillShowNotification == type) 
    {
        self.keyboardShowNotification = notif; // implicit copy
        dispatcher->dispatchKeyboardWillShow(notiInfo);
    }
    else if (UIKeyboardDidShowNotification == type)
    {
        //CGSize screenSize = self.window.screen.bounds.size;
        dispatcher->dispatchKeyboardDidShow(notiInfo);
        caretRect_ = end;
        caretRect_.origin.y = viewSize.height - (caretRect_.origin.y + caretRect_.size.height + [UIFont smallSystemFontSize]);
        caretRect_.size.height = 0;
        isKeyboardShown_ = YES;
    }
    else if (UIKeyboardWillHideNotification == type)
    {
        dispatcher->dispatchKeyboardWillHide(notiInfo);
    }
    else if (UIKeyboardDidHideNotification == type)
    {
        caretRect_ = CGRectZero;
        dispatcher->dispatchKeyboardDidHide(notiInfo);
        isKeyboardShown_ = NO;
    }
}

-(void) doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)dis
{
    [UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:duration];
	[UIView setAnimationBeginsFromCurrentState:YES];
    
    //NSLog(@"[animation] dis = %f,scale = %f \n",dis,cocos2d::CCEGLView::sharedOpenGLView()->getScaleY());
    
    if (dis < 0.0f) dis = 0.0f;

	dis *= cocos2d::CCEGLView::sharedOpenGLView()->getScaleY();
    
    if (self.contentScaleFactor == 2.0f)
    {
        dis /= 2.0f;
    }
    
    self.frame = CGRectMake(originalRect_.origin.x,originalRect_.origin.y - dis,originalRect_.size.width,originalRect_.size.height);
    switch ([[UIApplication sharedApplication] statusBarOrientation])
    {
        case UIInterfaceOrientationPortrait:
            self.frame = CGRectMake(originalRect_.origin.x,originalRect_.size.height);
            break;
            
        case UIInterfaceOrientationPortraitUpsideDown:
            self.frame = CGRectMake(originalRect_.origin.x,originalRect_.origin.y + dis,originalRect_.size.height);
            break;
            
        case UIInterfaceOrientationLandscapeLeft:
            if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
            {
                self.frame = CGRectMake(originalRect_.origin.x - dis,originalRect_.origin.y,originalRect_.size.height);
            }
            else{
                self.frame = CGRectMake(originalRect_.origin.x,originalRect_.size.height);
            }
            break;
        case UIInterfaceOrientationLandscapeRight:
            if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
            {
                self.frame = CGRectMake(originalRect_.origin.x + dis,originalRect_.size.height);
            }
            break;
            
        default:
            break;
    }
    
	[UIView commitAnimations];
}

修改更换这两个函数 在IOS EAGLView.mm中

实现原理就是验证版本号。当然通过验证版本号这种方式不是很好。

3.3 已经修改过了。

猜你在找的Cocos2d-x相关文章