2012-07-27 6 views
0

내 앱에서 iPad의 방향을 감지하고 위치와 위치를 잘 조정하여 세로 및 가로 방향으로 잘 작동하도록했습니다.가상 키보드에 애니메이션 적용

내 방향을 고려하지 않은 가상 키보드 방법 내 문제 : 한 번 사용자가 텍스트 상자 중 하나를 누릅니다 방법은 키보드를위한 공간을 만들고 텍스트 상자를 볼 수있는 볼 수 있습니다.) 그러나 사용자가 풍경에있을 때 텍스트 상자를 살짝 두드리면 애니메이션이 마치 세로로 보이는 것처럼 위로가 아니라 옆으로 밀어 넣을 수 있습니다.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

는 어떻게하면 올바르게 애니메이션 방법을 변경해야합니다 :

- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; 

    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; 
    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; 
    CGFloat heightFraction = numerator/denominator; 
    if (heightFraction < 0.0) { 
     heightFraction = 0.0; 
    } 
    else if (heightFraction > 1.0) { 
     heightFraction = 1.0; 
    } 

    UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation]; 

    if (orientation == UIInterfaceOrientationPortrait || 
     orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
    } 
    else { 
     animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 
    } 

    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y -= animatedDistance; 
    // viewFrame.origin.x -= animatedDistance; 


    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 
    [self.view setFrame:viewFrame]; 
    [UIView commitAnimations]; 
} 

나는 또한이 (내가 아이 패드를 거꾸로 잡아 경우 아래 대신 최대의 내보기 프레임을 같은 밀어 버린다) 장치가 방향을 바꿉니 까?

감사합니다.

답변

1

방향을 감지하고 해당 정보를 기반으로보기를 이동할 방향을 결정하십시오.

if ([UIDevice currentDevice].orientation==UIDeviceOrientationLandscapeLeft) 
    viewFrame.origin.x -= animatedDistance; 
if ([UIDevice currentDevice].orientation==UIDeviceOrientationLandscapeRight) 
    viewFrame.origin.x += animatedDistance; 
if ([UIDevice currentDevice].orientation==UIDeviceOrientationPortrait) 
    viewFrame.origin.y -= animatedDistance; 
if ([UIDevice currentDevice].orientation==UIDeviceOrientationPortraitUpsideDown) 
    viewFrame.origin.y += animatedDistance; 
+0

코드 내에 viewFrame.origin.y - = animatedDistance; 그래서 그것은 내가보기에 만약 당신이 내가 원한다면 (origin.y는 원산지가 옆에있는 것처럼) 행동하는 것을 보았을 때, 내가보기에는 내가 초상화에있을 때보기를 들어 올려야하지만, 가로로 보면 옆으로 향한다. 나는 무엇인지 모르겠다. 누락 된 –

+0

그 이유는 위의 x를 사용하고 있습니다. – Dustin

+0

은 매력처럼 작동합니다. (정보 제공을 위해 일단 텍스트 상자가 사직되면이 작업을 취소해야합니다.) 감사합니다. –

관련 문제