简体   繁体   English

旋转后UIView的位置

[英]position of UIView after rotation

I have a custom UIView subclass without a corresponding UIViewController. 我有一个没有对应的UIViewController的自定义UIView子类。 I use this class in various places in my app. 我在应用程序的各个地方使用了此类。 When I rotate the device I need to reposition the view. 旋转设备时,我需要重新定位视图。 I understand this is normally done with the willAnimateRotationToInterfaceOrientation:duration: method, but that's a part of the UIViewController class and not UIView. 我了解这通常是通过willAnimateRotationToInterfaceOrientation:duration:方法完成的,但这是UIViewController类的一部分,而不是UIView。 My question is, is there a way for my UIView to reposition itself without having to implement a corresponding UIViewController? 我的问题是,有没有一种方法可以让我的UIView重新定位自身而无需实现相应的UIViewController?

you application receives notifications from the device when it rotates. 您的应用程序旋转时会收到来自设备的通知。 you can tell the device to begin generating these notifications for you using 您可以使用以下命令告诉设备开始为您生成这些通知

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

and then you can register for the notification using 然后您可以使用来注册通知

[NSNotificationCenter default] addObserver:self selector:@selector(doThisWhenRotates:) name:addObserver:selector:name:object: object:nil]; [NSNotificationCenter默认值] addObserver:自我选择器:@selector(doThisWhenRotates :) name:addObserver:selector:name:object:object:nil];

HOWEVER, i wouldn't recommend that. 但是,我不建议这样做。 if you really want to follow MVC, you should have a controller to handle this type of stuff for the view. 如果您真的想遵循MVC,则应该有一个控制器来处理视图的此类内容。

but if you need to, you can do it the way listed above. 但是如果需要,可以按照上面列出的方法进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM