gpt4 book ai didi

iphone - 以编程方式使 uiview 适应 iPad/iPhone 屏幕

转载 作者:行者123 更新时间:2023-12-03 21:16:42 29 4
gpt4 key购买 nike

我制作了一个应用程序,其中有许多从 UIView 子类化的 View 。这些 View 的大小和方向是随机的,并且可以保存应用程序屏幕的状态。当用户在打开屏幕的同一设备上保存屏幕时,屏幕状态为“正常”。一切都已正确定位。但是,如果用户在 iPhone 上保存屏幕状态并从 iPad 打开它,则 View 的位置将不正确。实际上, View 看起来更短或更长,中心似乎已正确保存,但 View 的旋转及其大小(边界属性)无法正常工作。

这是保存和恢复 View 状态的两种方法

- (void)encodeWithCoder:(NSCoder *)aCoder {
// Save the screen size of the device that the view was saved on
[aCoder encodeCGSize:self.gameView.bounds.size forKey:@"saveDeviceGameViewSize"];

// ****************
// ALL properties are saved in normalized coords
// ****************

// Save the center of the view
CGPoint normCenter = CGPointMake(self.center.x / self.gameView.bounds.size.width, self.center.y / self.gameView.bounds.size.height);
[aCoder encodeCGPoint:normCenter forKey:@"center"];

// I rely on view bounds NOT frame
CGRect normBounds = CGRectMake(0, 0, self.bounds.size.width / self.gameView.bounds.size.width, self.bounds.size.height / self.gameView.bounds.size.height);
[aCoder encodeCGRect:normBounds forKey:@"bounds"];

// Here I save the transformation of the view, it has ONLY rotation info, not translation or scalings
[aCoder encodeCGAffineTransform:self.transform forKey:@"transform"];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {

// Restore the screen size of the device that the view was saved on
saveDeviceGameViewSize = [aDecoder decodeCGSizeForKey:@"saveDeviceGameViewSize"];

// Adjust the view center
CGPoint tmpCenter = [aDecoder decodeCGPointForKey:@"center"];
tmpCenter.x *= self.gameView.bounds.size.width;
tmpCenter.y *= self.gameView.bounds.size.height;
self.center = tmpCenter;

// Restore the transform
self.transform = [aDecoder decodeCGAffineTransformForKey:@"transform"];

// Restore the bounds
CGRect tmpBounds = [aDecoder decodeCGRectForKey:@"bounds"];
CGFloat ratio = self.gameView.bounds.size.height / saveDeviceGameViewSize.height;
tmpBounds.size.width *= (saveDeviceGameViewSize.width * ratio);
tmpBounds.size.height *= self.gameView.bounds.size.height;
self.bounds = tmpBounds;
}
return self;
}

最佳答案

如何分离 iPhone 和 iPad 版本之间的 View 状态?对于每个设备,如果该设备的配置尚未保存,是否只使用默认值?我还没有看到你的 UI,但我认为一般来说像这样的解决方案会很好地工作,更容易实现,并且也符合用户的期望。

关于iphone - 以编程方式使 uiview 适应 iPad/iPhone 屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11773010/

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