方向问题

发布于 2024-11-29 04:46:13 字数 2048 浏览 8 评论 0原文

我希望我的文本字段应该在任何方向上移动。

但我在横向右侧和纵向颠倒时遇到问题。

当我将其定向到其中任何一个时,我的文本字段向下,视图不会上升,但顶部的文本字段会上升。

建议我一些解决方案。

下面是我用于定位的代码。

如果可以的话,请建议我整个过程,因为我是 iPhone 开发的新手。

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

CGRect viewFrame;
UIInterfaceOrientation orientation=[[UIApplication sharedApplication]statusBarOrientation];
//code for text field editing in landscape an portrait mode

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

if (orientation==UIInterfaceOrientationPortrait) {
    viewFrame=self.view.frame;
    viewFrame.origin.y-=animatedDistance;
}
else if(orientation==UIInterfaceOrientationPortraitUpsideDown){
    viewFrame=self.view.frame;
    viewFrame.origin.y+=animatedDistance;
}
else if(orientation == UIInterfaceOrientationLandscapeRight){
    viewFrame=self.view.frame;
    viewFrame.origin.x+=animatedDistance;

}
else if(orientation == UIInterfaceOrientationLandscapeLeft){

    viewFrame=self.view.frame;
    viewFrame.origin.x-=animatedDistance;
}

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

[UIView commitAnimations];
}

i want that my textfield should move up in any orientation.

But I am having a problem in landscape right and portrait upside down.

As when i orient it to any one of them my textfield down the view does not go up but the textfield which is on top is going up.

suggest me some solution.

below is the code that i am using for orientation.

please suggest me whole procedure if you can as i am a niwBie to iPhone development.

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

CGRect viewFrame;
UIInterfaceOrientation orientation=[[UIApplication sharedApplication]statusBarOrientation];
//code for text field editing in landscape an portrait mode

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

if (orientation==UIInterfaceOrientationPortrait) {
    viewFrame=self.view.frame;
    viewFrame.origin.y-=animatedDistance;
}
else if(orientation==UIInterfaceOrientationPortraitUpsideDown){
    viewFrame=self.view.frame;
    viewFrame.origin.y+=animatedDistance;
}
else if(orientation == UIInterfaceOrientationLandscapeRight){
    viewFrame=self.view.frame;
    viewFrame.origin.x+=animatedDistance;

}
else if(orientation == UIInterfaceOrientationLandscapeLeft){

    viewFrame=self.view.frame;
    viewFrame.origin.x-=animatedDistance;
}

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

[UIView commitAnimations];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2024-12-06 04:46:13

在您的 ViewController 中重写 - willRotateToInterfaceRotation:duration 方法。当用户旋转设备时调用此消息。在这种方法中,您可以将所有视图设置为放置在适当的位置。请参阅参考中的响应视图旋转事件: UIViewController 参考

你可以做这样的事情:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown){
        yourView.frame = CGRectMake(x,y,w,h);  //and set the proper coordinate for this view in this orientation
}

然后对所有未放置的视图执行此操作旋转时正确

In your ViewController override the - willRotateToInterfaceRotation:duration method. This message is called when the user rotates the device. In this method you can then set all your views to be placed in their proper positions. See Responding to View Rotation Events in the reference: UIViewController reference

you could do something like this:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown){
        yourView.frame = CGRectMake(x,y,w,h);  //and set the proper coordinate for this view in this orientation
}

and then do it for all your views that are not placed correctly when rotating

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文