gpt4 book ai didi

iphone - 如何在方向更改后调整 UIView 的大小(包含的代码似乎不起作用)

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:56:22 26 4
gpt4 key购买 nike

为什么这个 UIView 布局代码不能像我想要的那样工作?

背景:

  • 我的 UIViewController 中有一个自定义 UIView
  • 自定义 UIView 有一个时钟背景 ImageView 和一个时针 ImageView
  • 在引入一些代码(见下文)以尝试调整时针的大小以进行方向更改后,我遇到了困难。
  • 下面的代码确实把事情搞砸了——沙漏指针的边框与这段代码相去甚远

任何想法我做错了什么 - 显然有一些假设或误解我必须让它像这样搞砸......

UIViewController 调用的代码(在“didRotateFromInterfaceOrientation”方法中)到我的自定义 View :

// Centre image 
self.hourHandImageView.center = self.hourFaceUIImageView.center;

// Resize to cater for either Portrait or Landscape orientation
CGSize currSize = self.hourFaceUIImageView.frame.size;
float newHourHandImageheightWidth = currSize.width < currSize.height ? currSize.width : currSize.height;
CGSize newHourHandViewSize = CGSizeMake(newHourHandImageheightWidth, newHourHandImageheightWidth);
CGRect newRect = self.hourHandImageView.frame;
newRect.size = newHourHandViewSize;
self.hourHandImageView.frame = newRect;

附言。如果没有“调整大小以适应纵向或横向方向”代码,时针会正确地居中放置(注意我使用转换来扭转它)。我真正需要做的就是在方向改变后适当调整时针的大小,注意小时背景图像在横向模式下较小。

最佳答案

首先,如何判断您是纵向还是横向?有了这个(?):

float newHourHandImageheightWidth = currSize.width < currSize.height ? currSize.width : currSize.height;

我建议你做这样的事情:

-

(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
[self reOrderToPortrait];
} else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
[self reOrderToLandScape];
}
}

然后在每个方法上,您应该为您的 View 定义新的框架。首先从您的 View 中删除自动调整大小,因为您将定义新的框架,并且您不希望它受到干扰。您还可以像这样定义一些措施:

#define hourHandImageViewPortrait CGPointMake(...,...)
#define hourHandImageViewLandScape CGPointMake(..., ...)

关于iphone - 如何在方向更改后调整 UIView 的大小(包含的代码似乎不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6673773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com