gpt4 book ai didi

objective-c - iPad/iPhone 旋转时旋转 subview

转载 作者:行者123 更新时间:2023-11-29 05:00:10 25 4
gpt4 key购买 nike

我有一个以编程方式创建的状态/叠加 View ,当设备忙于执行某些任务时会弹出该 View 。我正在使用 [view addSubview: statusView] 将其添加到当前 View 。

当设备旋转时,statusView 会随设备旋转,因此文本会横向移动。我该如何更改以确保 subview 不会旋转,而是重新定位?

有一个similar SO question here这表明我可能需要实现一些方法来处理调整大小/重新定位,但它没有详细说明。

到目前为止,我正在屏幕外初始化 uiviewcontroller,如下所示,并在需要时将其移动到屏幕上:

- (id)initWithStatus:(NSString*)_status forWindow: (UIWindow*) window {
if (self = [super init]) {
initialFrame = window.frame;
visible = NO;
barHeight = 40;

// Create the status bar below the windows drawing area
height = window.bounds.size.height;
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, height, window.bounds.size.width, barHeight)] autorelease];
[self.view setBackgroundColor:[UIColor blackColor]];
[self.view setAlpha:.5];

// Add text text
status = [[UILabel alloc] initWithFrame:CGRectMake(7, 7, window.bounds.size.width-14, barHeight-14)];
status.font = [UIFont systemFontOfSize: 14];
status.text = _status;
status.textAlignment = UITextAlignmentCenter;
status.backgroundColor = [UIColor clearColor];
status.textColor = [UIColor whiteColor];
[self.view addSubview:status];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];

}

return self;
}

在纵向时,可以使用以下方法轻松将其滑动到屏幕上:

-(void)updateStatus:(NSString*)_status {
if(visible) {
status.text = _status;
return;
}

visible = YES;
status.text = _status;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];

CGRect frame = self.view.frame;
frame.origin.y = height - barHeight;
self.view.frame = frame;
[UIView commitAnimations];
}

因此,当检测到方向变化时,这称为:

- (void)orientationChanged:(NSNotification *)notification {
// Could just resize the box based on the new orientation but the text inside the box
// runs the wrong direction instead of along the box.
}

最佳答案

如果您使用 UIViewController,则可以利用 autorotation .如果您的 View 不属于 View Controller ,通常您的自动调整大小蒙版将确保 View 仍然正确居中。如果您想手动重新定位它,可以通过观察 UIDeviceOrientationDidChangeNotification 来实现。 .

关于objective-c - iPad/iPhone 旋转时旋转 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7172143/

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