简体   繁体   English

将旋转事件捕获到UIInputViewController中

[英]Catch rotation event into UIInputViewController

I'm developing a custom Keyboard and I'm trying to catch a device rotation event. 我正在开发自定义键盘,并且试图捕获设备旋转事件。

I've already tried : 我已经尝试过了:

override func viewDidLayoutSubviews() {
    print("Foo")
}

and

override func viewWillLayoutSubviews() {
    print("🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳 Bar")
}

and

override func didRotateFromInterfaceOrientation(sender : UIInterfaceOrientation){ 
    print("🐳🐳🐳🐳");
}

and

 override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    print("Bar")
}

and

override func updateViewConstraints() {
    super.updateViewConstraints()
    print("🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳🐳 Foo")
}

But it's not working. 但这不起作用。

iPad Air 2 Simulator - iOS9 iPad Air 2模拟器-iOS9

Any idea ? 任何想法 ?

Most of the methods related to rotation are deprecated now. 现在不推荐使用大多数与旋转有关的方法。 Apple is moving towards sizes and traits changes notifications, and not so much direct device orientation change notifications. 苹果正朝着尺寸和特征更改通知的方向发展,而不是直接面向设备方向的更改通知。

Also, UIDevice.currentDevice().orientation always returns Unknown in keyboard extensions with both Full Access and beginGeneratingDeviceOrientationNotifications. 同样,在具有完全访问权限和beginGeneratingDeviceOrientationNotifications的键盘扩展中,UIDevice.currentDevice()。orientation始终返回Unknown。

The correct method (and the one listed as replacement for the deprecated methods in the documentation) is 正确的方法(以及替代文档中不推荐使用的方法的方法)是

- viewWillTransitionToSize:withTransitionCoordinator:

It's strange that it didn't work for you. 奇怪的是它对您不起作用。 I just tried it in an UIInputViewController on iPad Air 2 iOS9 Simulator and it works: 我只是在iPad Air 2 iOS9 Simulator的UIInputViewController中尝试过,它的工作原理是:

2015-10-13 12:28:15.347 [Info] [KeyboardViewController.swift:60] viewWillTransitionToSize( :withTransitionCoordinator:) > Rotate to size (1024.0, 313.0) 2015-10-13 12:28:20.512 [Info] [KeyboardViewController.swift:60] viewWillTransitionToSize( :withTransitionCoordinator:) > Rotate to size (768.0, 313.0) 2015-10-13 12:28:15.347 [Info] [KeyboardViewController.swift:60] viewWillTransitionToSize( :withTransitionCoordinator :)>旋转至大小(1024.0,313.0)2015-10-13 12:28:20.512 [Info] [KeyboardViewController .swift:60] viewWillTransitionToSize( :withTransitionCoordinator :)>旋转至大小(768.0,313.0)

Are you debugging the correct target? 您是否在调试正确的目标? To see console messages from your extension, you must set it as the run target: 要查看扩展中的控制台消息,必须将其设置为运行目标: 在此处输入图片说明

Note that there is also 请注意,还有

- willTransitionToTraitCollection:withTransitionCoordinator:

But this one won't work for the iPad, because the iPad is always Regular,Regular for both landscape and portrait. 但这不适用于iPad,因为iPad始终对风景和人像都是“常规”,“常规”。

Not all view controllers will receive the transition events. 并非所有的视图控制器都将接收过渡事件。 I think it depends on whether your view controller is inside a container controller like a UINavigationController or UITabBarController and whether they pass the change on to their child controllers or not. 我认为这取决于您的视图控制器是否在UINavigationController或UITabBarController之类的容器控制器内部,以及它们是否将更改传递给其子控制器。

A guaranteed method of receiving orientation changes would be to register for UIDevice orientation change notifications. 接收方向更改的保证方法是注册UIDevice方向更改通知。 See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/#//apple_ref/c/data/UIDeviceOrientationDidChangeNotification for the list of notification events. 有关通知事件的列表,请参见https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/#//apple_ref/c/data/UIDeviceOrientationDidChangeNotification

Here is what you would do in ObjC. 这是您在ObjC中要做的事情。 Should translate easy enough to Swift. 应该足够容易地转换为Swift。

In your app delegate activate orientation changes using: 在您的应用程序委托中,使用以下方法激活方向更改:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

In the view controller you want notifications add methods to handle the notification, add and remove observing the notification. 在视图控制器中,您希望通知添加方法来处理通知,添加和删除观察通知。

// Method to be called when orientation notification is changed.
- (void)orientationDidChange:(NSNotification *)notification { 
  // Add your code to deal with the orientation.
  // You can get the current orientation for UIDevice as follows:

  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
}

// Adds observation of orientation change
-(void)addObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
     selector:@selector(orientationDidChange:) 
     name:@"UIDeviceOrientationDidChangeNotification" 
     object:nil];
}

// Removes observation of orientation change
-(void)removeObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

Add calls to addObserver and removeObserver in an appropriate place. 在适当的位置添加对addObserverremoveObserver调用。 viewDidAppear and viewDidDisappear for example. 例如, viewDidAppearviewDidDisappear

It looks like this works in my project with iOS 9 and iPad Air 2: 看来这在我的iOS 9和iPad Air 2项目中适用。

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    print("hi")
}

it looks like you tried that but it didn't work though... 看起来您尝试过,但是却没有效果...

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

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