我有一个 UIView,它应该覆盖整个设备(UIWindow)以支持图像放大/缩小效果,我正在使用核心动画,其中用户点击 UITableViewCell 上的按钮并缩放关联的图像。
缩放性能完美无缺,我无法弄清楚为什么即使设备处于横向状态,子视图仍处于纵向模式。下图:
我确实有一个导航控制器,但这个视图已直接添加到 UIWindow 中。
我有一个 UIView,它应该覆盖整个设备(UIWindow)以支持图像放大/缩小效果,我正在使用核心动画,其中用户点击 UITableViewCell 上的按钮并缩放关联的图像。
缩放性能完美无缺,我无法弄清楚为什么即使设备处于横向状态,子视图仍处于纵向模式。下图:
我确实有一个导航控制器,但这个视图已直接添加到 UIWindow 中。
您可以在此处阅读一些可能的原因:
技术问答 QA1688 - 为什么我的 UIViewController 不会随设备旋转?
在您的情况下,这可能是您将视图作为另一个子视图添加到窗口的事实。只有第一个子视图获取旋转事件。您可以做的是将它添加为第一个窗口子视图的子视图。
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:myView];
从 iOS 6 开始,只有最顶层的视图控制器(与 UIApplication 对象一起)参与决定是否旋转以响应设备方向的变化。
https://developer.apple.com/library/content/qa/qa1688/_index.html
我开源了一个名为AGWindowView的 pod 。
它会自动处理任何旋转和帧变化,所以你不必担心。
它支持 SDK 和 iOS 系统版本的任意组合。相关代码可以在这里找到: https ://github.com/hfossli/AGWindowView/blob/master/Source/AGWindowView.m
我在 UIApplication 上创建了一个类别,它有一个帮助属性和获取 keyWindow 的第一个子视图的方法。无论如何,这是您要覆盖的视图。现在,当您将由 UIViewController 管理的视图添加到该视图时,将调用 shouldRotateToInterfaceOrientation: 方法。
UIApplication+WindowOverlay.h
#import <UIKit/UIKit.h>
@interface UIApplication(WindowOverlay)
@property (nonatomic, readonly) UIView *baseWindowView;
-(void)addWindowOverlay:(UIView *)view;
@end
UIApplication+WindowOverlay.m
#import "UIApplication+WindowOverlay.h"
@implementation UIApplication(WindowOverlay)
-(UIView *)baseWindowView{
if (self.keyWindow.subviews.count > 0){
return [self.keyWindow.subviews objectAtIndex:0];
}
return nil;
}
-(void)addWindowOverlay:(UIView *)view{
[self.baseWindowView addSubview:view];
}
@end
这就是你将如何使用它。
//at the top of the file...or in {yourproject}.pch
#import "UIApplication+WindowOverlay.h
//in a method:
UIView *view = [UIView new];
UIView *window = [UIApplication sharedApplication].baseWindowView;
view.frame = window.bounds;
[window addSubview:view];
//or
[[UIApplication sharedApplication] addWindowOverlay:view];
这是因为正如您提到的,您的视图已直接添加到 UIWindow,因此当为导航控制器调用旋转方法时,uiview 没有任何反应。如果 UIView 是视图控制器视图的子视图,它会旋转。如果由于某种原因无法做到这一点。然后你可以覆盖这个方法:
// This method is called every time the device changes orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
}
每次你的方向改变时,你的视图方向也会改变。
我在将视图直接添加到窗口时遇到了类似的问题。也许这会有所帮助:添加到窗口后自动调整 UIView 的大小
我如何解决这个问题的另一个解决方案。
定义当前方向:
@interface AJImageCollectionViewController (){
UIInterfaceOrientation _currentOrientation;
}
@end
然后在 viewWillLayoutSubviews 中检查方向:
- (void)viewWillLayoutSubviews {
[self checkIfOrientationChanged];
}
- (void)checkIfOrientationChanged {
UIInterfaceOrientation newOrientation = [[UIApplication sharedApplication] statusBarOrientation];
BOOL newOrientationIsPortrait = UIInterfaceOrientationIsPortrait(newOrientation);
BOOL oldOrientationIsPortrait = UIInterfaceOrientationIsPortrait(_currentOrientation);
// Check if the orientation is the same as the current
if(newOrientationIsPortrait != oldOrientationIsPortrait){
_currentOrientation = newOrientation;
// Do some stuff with the new orientation
}
}