繁体   English   中英

如何检测以编程方式生成的UIView的旋转

[英]How to detect rotation for a programatically generated UIView

我有一个以编程方式创建的UIView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];

        UIImageView* closeButton = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"modal_close.png"]];

        closeButton.frame = CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09, closeButton.frame.size.width, closeButton.frame.size.height);

        UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09,20, 20)];

        [button addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];

        UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width*.1,self.frame.size.height*.1,self.frame.size.width*.8,self.frame.size.height*.8)];

        mainView.backgroundColor = [UIColor whiteColor];
        _displayMainView = mainView;        

        [self addSubview:_displayMainView];
        [self addSubview:closeButton];
        [self addSubview:button];

        mainView = nil;
        closeButton = nil;
    }
    return self;
}

如何检测旋转? 这是作为另一个现有视图之上的模态。 这是一款仅限iPad的应用,如果重要的话。

您可以让设备开始生成轮换通知:

  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

添加您自己的选择器作为设备轮换通知的观察者:

      [[NSNotificationCenter defaultCenter] addObserver: self selector:   @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];

然后编写通知处理程序

- (void)deviceOrientationDidChange:(NSNotification *)notification {
     //Obtain current device orientation
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

      //Do my thing
 }

记得在调用自定义UIView的dealloc方法时(或者在完成时)删除观察者,并停止生成设备更改通知。

-(void) dealloc{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}

这对你的问题有帮助吗?

我想你有两个选择:

  1. 您将视图放在自定义UIViewController并覆盖shouldAutorotateToInterfaceOrientation:方法,以便为要支持的所有方向返回YES。 视图控制器将自动旋转并调整内容大小(您的视图)。 你只需要确保有正确的autoresizeMask(或约束,如果你使用autolayout)。
  2. 您可以使用加速度计直接观察设备方向的变化。 然后,您可以在需要时调整视图。

如果可以的话,这是你应该选择的第一种方法。

我正在重写@ clearwater82对Swift 3的回答,因为他的方法是我最终使用的方法:

我在视图的init方法中添加了以下代码(对于控制器,它将在它的viewDidLoad方法中):

// Handle rotation
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(
    self,
    selector: #selector(self.orientationChanged(notification:)),
    name: NSNotification.Name.UIDeviceOrientationDidChange,
    object: nil
)


然后我将orientationChanged方法添加到我的视图中。 您可以根据需要重命名此方法,只需记住在上面的代码段中重命名它:

// Called when device orientation changes
@objc func orientationChanged(notification: Notification) {
    // handle rotation here
}


最后,我添加了deinit方法,以便在删除视图时删除不必要的通知:

deinit {
    NotificationCenter.default.removeObserver(self)
    UIDevice.current.endGeneratingDeviceOrientationNotifications()
}

最后一个片段的作用是从通知观察者中删除视图本身,然后停止设备生成其他旋转通知。 这种方法是我所需要的,但您可能不希望在删除视图时停止生成通知。

我希望这很有用,欢呼

暂无
暂无

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

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