2011-05-24 44 views
0

我有來自Cocoa with Love的代碼,當鍵盤出現時滾動UITextField,以至於鍵盤不覆蓋UITextField。在鍵盤顯示時在iPad上滑動UITextField

下面的代碼:

- (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; 

    // Issue is that numerator isn't big enough for bottom 3rd of the screen 

    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; 

    NSLog(@"Midline: %g Fraction: %g/%g", midline, 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; 


    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.view setFrame:viewFrame]; 

    [UIView commitAnimations]; 
} 

出於調試目的,我輸出的中線,等等看發生了什麼事。這是從頂部到底部的倒扣。您可以看到第6個分子下降爲負數,因爲爲其父視圖轉換的UITExtField.size.height是一個低數字。我無法弄清楚爲什麼這個數字是負面的。當你沿着視線前進時,身高應該與所有其他人一樣。

2011-05-24 09:36:08.600嬰兒布盧姆[27794:207]中線= 246 + 0.5 * 167

2011-05-24 09:36:08.601嬰兒布盧姆[27794:207]中線:329.5餾分:22.3/409.6

2011-05-24 09:36:09.535嬰兒布盧姆[27794:207]中線= 246 + 0.5 * 167

2011-05-24 09:36:09.536嬰兒布盧姆[27794:207]中線:329.5分數:22.3/409.6

2011-05-24 09:36:09.929寶寶布盧姆[27794:207]中線= 246 + 0.5 * 246

2011-05-24 09:36:09.930嬰兒布盧姆[27794:207]中線:369餾分:61.8/409.6

2011-05-24 09:36:10.313嬰兒布盧姆[27794:207]中線= 246 + 0.5 * 246

2011-05-24 09:36:10.314嬰兒布盧姆[27794:207]中線:369餾分:61.8/409.6

2011-05-24 09:36:10.793寶貝布盧姆[27794:207]中線= 246 + 0.5 * 97

2011-05-24 09:36:10.794寶貝布盧姆[27794:207]中線:294.5分數:-12.7/409.6

2011-05-24 09:36:11.785嬰兒布盧姆[27794:207]中線= 246 + 0.5 * 148

2011-05-24 09:36:11.786嬰兒布盧姆[27794:207]中線:320分數:12.8/409.6

回答

2

原來,當在iPad上的橫向它切換原點(x現在y和寬度現在的高度。

下面是代碼,使其工作:

- (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]; 

    float origin; 
    float textFieldHeight; 
    float viewOrigin; 
    float viewHeight; 

    UIInterfaceOrientation orientation = 
    [[UIApplication sharedApplication] statusBarOrientation]; 


    if (orientation == UIInterfaceOrientationPortrait || 
     orientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     textFieldHeight = textFieldRect.size.height; 
     origin = textFieldRect.origin.y; 
     viewOrigin = viewRect.origin.y; 
     viewHeight = viewRect.size.height; 
    } 
    else 
    { 
     textFieldHeight = textFieldRect.size.width; 
     viewOrigin = viewRect.origin.x; 
     viewHeight = viewRect.size.width; 
     origin = viewHeight - textFieldRect.origin.x; 
    } 


    CGFloat midline = origin + 0.5 * textFieldHeight; 

    // Take the current middle y location of the textfield 
    // If it is in the top 20% of the view don't move it 
    // If it is in the middle 60% move it by a fraction of the keyboard size 
    // If it is in the bottom 20% move it by the whole size of the keyboard 


    CGFloat numerator = midline - viewOrigin - MINIMUM_SCROLL_FRACTION * viewHeight; 

    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewHeight; 

    CGFloat heightFraction = numerator/denominator; 


    if (heightFraction < 0.0) 
    { 
     heightFraction = 0.0; 
    } 
    else if (heightFraction > 1.0) 
    { 
     heightFraction = 1.0; 
    } 

    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; 

    //NSLog(@"Denom: %g", denominator); 
    //NSLog(@"Distance: %g", animatedDistance); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.view setFrame:viewFrame]; 

    [UIView commitAnimations]; 
} 
1
origin = viewHeight - textFieldRect.origin.x; 

我認爲你不需要這個,因爲它使不正確的字段起源和非常大的中間值,其中執行滾動,在那裏不需要。 所以一般你應該只使用x origin作爲y。

origin = textFieldRect.origin.x; 
相關問題