アプリで iPad の向きを検出し、それに応じて位置を再調整して、縦向きと横向きでうまく機能するようにしました。
私が持っている仮想キーボードの方法における私の問題は、向きを考慮していません: 縦向きで、ユーザーがテキストボックスの1つをタップすると、メソッドはビューを持ち上げてキーボード用のスペースを作り、テキストボックスを表示します(カバーされていません)-ただし、横向きのユーザーがテキスト ボックスをタップすると、アニメーションは、縦向きのままであるかのように、ビューを上ではなく横に押します。(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;
}
デバイスの向きが変わったときに正しくアニメーション化する方法を変更するにはどうすればよいですか?
ありがとう