[英]animating the virtual keyboard
我讓我的應用檢測到iPad上的方向,並相應地重新調整其位置,以使其在縱向和橫向上都能正常工作。
我在虛擬鍵盤方法中遇到的問題是我沒有考慮方向:在縱向模式下,一旦用戶點擊其中一個文本框,該方法將抬起視圖以為鍵盤騰出空間,並使文本框可見(未覆蓋)-但是,當用戶處於橫向並點擊一個文本框時,動畫將把視圖推向側面而不是向上,就像它仍然是縱向的一樣。 (同樣,如果我將iPad倒置,則會將其視野框向下推,而不是向上推)
- (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];
}
我也有:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
當設備改變方向時,應如何更改方法以使其正確設置動畫?
謝謝
檢測方向,然后根據該信息確定將視圖移動到哪個方向。
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.