我有一个 UIViewController,它只在设备旋转时旋转其中的一些 subview 。这在 iOS7 下工作正常,但在 iOS8 下会中断。看来 UIView 的边界是通过 iOS8 下的转换来调整的。这是出乎意料的。
这是一些代码:
@interface VVViewController ()
@property (weak, nonatomic) IBOutlet UIView *pinnedControls;
@property (nonatomic, strong) NSMutableArray *pinnedViews;
@end
@implementation VVViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.pinnedViews = [NSMutableArray array];
[self.pinnedViews addObject:self.pinnedControls];
}
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[UIViewController rotatePinnedViews:self.pinnedViews forOrientation:self.interfaceOrientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation) && UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
[UIViewController rotatePinnedViews:self.pinnedViews forOrientation:toInterfaceOrientation];
}
}
@end
我们在 UIViewController 上创建了一个类别来处理这种行为。这是相关的代码:
@implementation UIViewController (VVSupport)
+ (void)rotatePinnedViews:(NSArray *)views forOrientation:(UIInterfaceOrientation)orientation {
const CGAffineTransform t1 = [UIViewController pinnedViewTansformForOrientation:orientation counter:YES];
const CGAffineTransform t2 = [UIViewController pinnedViewTansformForOrientation:orientation counter:NO];
[views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
// Rotate the view controller
view.transform = t1;
[view.subviews enumerateObjectsUsingBlock:^(UIView *counterView, NSUInteger idx, BOOL *stop) {
// Counter-rotate the controlsUIin the view controller
counterView.transform = t2;
}];
}];
}
+ (CGAffineTransform)pinnedViewTansformForOrientation:(UIInterfaceOrientation)orientation counter:(BOOL)counter {
CGAffineTransform t;
switch ( orientation ) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
t = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationLandscapeLeft:
t = CGAffineTransformMakeRotation(counter ? M_PI_2 : -M_PI_2);
break;
case UIInterfaceOrientationLandscapeRight:
t = CGAffineTransformMakeRotation(counter ? -M_PI_2 : M_PI_2);
break;
}
return t;
}
@end
这是 Nib 的样子:
Nib 中名为pinned的UIView是pinnedControls的IBOutlet:
当我在 iOS7 或 iOS8 下以纵向模式运行它时,我得到了这个:
我在横向模式下在 iOS7 下看到了预期的结果:
但是在 iOS8 (GM) 下,我没有得到这种行为。这是我看到的:
请注意,带有文本“Pinned Label”的 UILabel 的中心与固定 UIView 的底部保持距离,它没有改变大小以适应旋转。 UIView 的所有边缘都固定在 super View 的顶部、左侧、底部和右侧。
在我看来,在 iOS8 下,transform 属性与自动布局的交互方式不同。我在这里有点困惑。我知道我不能依赖框架。我可能只是开始手动设置边界,但这似乎是错误的做法,基本上是围绕自动布局进行结束。
最佳答案
所以这在过去的几天里让我发疯,我能够通过改变我的动画块中 setTransform 调用的时间来修复
转到横向时,我在设置新框架后设置变换。拍摄肖像时,我在设置新框架之前设置了变换。所有这些都在“animateWithDuration...”方法的动画块中
我不确定它是否会直接帮助你处理你的代码,但它可能会激发一些解决它的灵感,因为我们肯定有类似的问题
关于autolayout - 在 iOS8 下使用 CGAffineTransform 旋转时 UIView 不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25775460/