2014-10-02 74 views
2

我有一個UIViewController,稱爲OoviumController,它響應設備旋轉。 OoviumController是我的應用程序的UIWindow rootViewController。 UIWindow也是它自己的看法。 OoviumController的視圖是透明的,並且位於不應該旋轉的UIWindow視圖的頂部。如何防止自動旋轉UIWindow視圖

從iOS 2到iOS 7,這一切工作正常。但是,我正在嘗試使用iOS 8的XCode 6構建我的項目,並且我注意到現在除了OoviumController的視圖之外,UIWindow視圖也在旋轉,這會在我的應用程序中造成嚴重問題。

如何將功能返回至以前的工作方式?如何防止UIWindow與我的rootViewController視圖一起旋轉?

我也注意到,UIWindow文檔不再將其列爲繼承UIView,即使addSubview似乎仍然有效。這是一個文檔錯誤還是UIView的公共繼承被棄用?

+0

你知道如何做到這一點嗎? – 2015-07-27 17:20:35

+0

@theReverend不幸的是,我沒有找到一個簡單的方法來做到這一點。然而,我創建了一個蠻力機制來處理這個問題,方法是在頂部創建第二個UIWindow並在旋轉過程中將控件翻轉到該窗口,然後在旋轉完成時向後翻轉。我對atm時間有點緊張,但我會盡量在今晚晚些時候回答基礎知識。 – aepryus 2015-07-27 18:22:13

+0

@theReverend對不起,我忘記了你。我剛剛發佈了幾個月前的一些代碼。在測試中,我注意到我沒有完全完成我正在做的事情,還有一些怪癖。 – aepryus 2015-08-03 22:16:32

回答

0

我還沒有找到這個問題的快速,簡單或優雅的解決方案。然而,我發現了一個單調乏味的暴力解決方案。我在這個月前工作過,實際上並不認爲我完全完成了。但是,我將爲感興趣的任何人發佈基本框架工作。

基本概念是創建兩個UIWindow對象,一個旋轉,另一個不旋轉。然後在旋轉開始之前將所有控件從旋轉窗口翻轉到非旋轉窗口。旋轉完成後將它們翻轉回去:

- (void) hideHovers { 
    for (Hover* hover in self.hovers) 
     hover.alpha = 0; 
} 
- (void) showHovers { 
    for (Hover* hover in self.hovers) 
     hover.alpha = 1; 
} 

// Public ========================================================================================== 
- (void) addHover:(Hover*)hover { 
    [[Oovium aetherWindow] addSubview:hover]; 
    [self.hovers addObject:hover]; 
} 

- (void) beforeRotation { 
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    for (Hover* hover in self.hovers) { 
     hover.orientation = orientation; 
     [hover removeFromSuperview]; 
     [controlWindow_ addSubview:hover]; 
    } 
    controlWindow_.hidden = NO; 
    [self showHovers]; 
    [UIView animateWithDuration:0.5 animations:^{ 
     [self hideHovers]; 
    }]; 
} 
- (void) afterRotation { 
    for (Hover* hover in self.hovers) { 
     hover.transform = CGAffineTransformIdentity; 
     [hover removeFromSuperview]; 
     [[Oovium aetherWindow] addSubview:hover]; 
     [hover place]; 
    } 
    controlWindow_.hidden = YES; 
    [UIView animateWithDuration:0.5 animations:^{ 
     [self showHovers]; 
    }]; 
} 

其實,這是很容易的部分。困難的部分是處理控制的轉換。下面的代碼並不完全適用於所有旋轉。我會在完成之後對其進行修改,但我不確定何時能夠恢復。

// 
// Hover.m 
// Oovium 
// 
// Created by Joe Charlier on 2/22/15. 
// Copyright (c) 2015 Aepryus Software. All rights reserved. 
// 

#import "Controls.h" 
#import "Hover.h" 
#import "Oovium.h" 
#import "Utility.h" 

@interface Hover() 

@property (nonatomic) OOAnchor anchor; 
@property (nonatomic) CGPoint offset; 
@property (nonatomic) CGSize size; 

@end 

@implementation Hover 

// Static ========================================================================================== 
static CGPoint points_[5][5]; 
static CGPoint shift_[5]; 

+ (void) initialize { 
    CGFloat width = [Utility deviceBounds].size.width; 
    CGFloat height = [Utility deviceBounds].size.height; 

    CGPoint topLeft = CGPointMake(0,0); 
    CGPoint topRight = CGPointMake(width,0); 
    CGPoint bottomLeft = CGPointMake(0,height); 
    CGPoint bottomRight = CGPointMake(width,height); 
    CGPoint center = CGPointMake(width/2,height/2); 

    points_[UIInterfaceOrientationPortrait][OOAnchorTopLeft] = topLeft; 
    points_[UIInterfaceOrientationPortrait][OOAnchorTopRight] = topRight; 
    points_[UIInterfaceOrientationPortrait][OOAnchorBottomLeft] = bottomLeft; 
    points_[UIInterfaceOrientationPortrait][OOAnchorBottomRight] = bottomRight; 
    points_[UIInterfaceOrientationPortrait][OOAnchorCenter] = center; 

    points_[UIInterfaceOrientationLandscapeRight][OOAnchorTopLeft] = topRight; 
    points_[UIInterfaceOrientationLandscapeRight][OOAnchorTopRight] = bottomRight; 
    points_[UIInterfaceOrientationLandscapeRight][OOAnchorBottomLeft] = topLeft; 
    points_[UIInterfaceOrientationLandscapeRight][OOAnchorBottomRight] = bottomLeft; 
    points_[UIInterfaceOrientationLandscapeRight][OOAnchorCenter] = center; 

    points_[UIInterfaceOrientationPortraitUpsideDown][OOAnchorTopLeft] = bottomRight; 
    points_[UIInterfaceOrientationPortraitUpsideDown][OOAnchorTopRight] = bottomLeft; 
    points_[UIInterfaceOrientationPortraitUpsideDown][OOAnchorBottomLeft] = topRight; 
    points_[UIInterfaceOrientationPortraitUpsideDown][OOAnchorBottomRight] = topLeft; 
    points_[UIInterfaceOrientationPortraitUpsideDown][OOAnchorCenter] = center; 

    points_[UIInterfaceOrientationLandscapeLeft][OOAnchorTopLeft] = bottomLeft; 
    points_[UIInterfaceOrientationLandscapeLeft][OOAnchorTopRight] = topLeft; 
    points_[UIInterfaceOrientationLandscapeLeft][OOAnchorBottomLeft] = bottomRight; 
    points_[UIInterfaceOrientationLandscapeLeft][OOAnchorBottomRight] = topRight; 
    points_[UIInterfaceOrientationLandscapeLeft][OOAnchorCenter] = center; 

    shift_[OOAnchorTopLeft] = CGPointMake(0, 0); 
    shift_[OOAnchorTopRight] = CGPointMake(1, 0); 
    shift_[OOAnchorBottomLeft] = CGPointMake(0, 1); 
    shift_[OOAnchorBottomRight] = CGPointMake(1, 1); 
    shift_[OOAnchorCenter] = CGPointMake(0.5, 0.5); 
} 
// ================================================================================================= 

+ (CGRect) placeInRect:(CGRect)rect anchor:(OOAnchor)anchor offset:(CGPoint)offset size:(CGSize)size { 
    CGPoint shift = shift_[anchor]; 
    CGFloat x = shift.x*(rect.size.width-size.width)+offset.x; 
    CGFloat y = shift.y*(rect.size.height-size.height)+offset.y; 
    return CGRectMake(x, y, size.width, size.height); 
} 

- (id) initWithAnchor:(OOAnchor)anchor offset:(CGPoint)offset size:(CGSize)size { 
    if (self = [super initWithFrame:[Hover placeInRect:[UIScreen mainScreen].bounds anchor:anchor offset:offset size:size]]) { 
     self.anchor = anchor; 
     self.offset = offset; 
     self.size = size; 
     self.backgroundColor = [UIColor purpleColor]; 
    } 
    return self; 
} 

- (void) setOrientation:(UIInterfaceOrientation)orientation { 
    _orientation = orientation; 

    CGPoint aO = self.frame.origin; 
    CGPoint aN = points_[orientation][_anchor]; 

    double w = self.size.width; 
    double h = self.size.height; 
    double q = (self.size.height-self.size.width)/2; 
    double ox = self.offset.x; 
    double oy = self.offset.y; 
    double ax = aN.x-aO.x; 
    double ay = aN.y-aO.y; 

    double dx=0; 
    double dy=0; 
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { 
     dx += q; 
     dy += q; 
    } 
    switch (orientation) { 
     case UIInterfaceOrientationPortrait: 
      dx = 0; 
      dy = 0; 
      break; 
     case UIInterfaceOrientationPortraitUpsideDown: 
      switch (_anchor) { 
       case OOAnchorTopLeft: 
        dx += -ax+w+ox; 
        dy += -ay+h+oy; 
        break; 
       case OOAnchorTopRight: 
        dx += -ax+ox; 
        dy += -ay+h+oy; 
        break; 
       case OOAnchorBottomLeft: 
        dx += -ax+w+ox; 
        dy += -ay+oy; 
        break; 
       case OOAnchorBottomRight: 
        dx += -ax+ox; 
        dy += -ay+oy; 
        break; 
      } 
      break; 
     case UIInterfaceOrientationLandscapeRight: 
      switch (_anchor) { 
       case OOAnchorTopLeft: 
        dx += 0; 
        dy += ax-h-ox; 
        break; 
       case OOAnchorTopRight: 
        dx += -ay+w+oy; 
        dy += ax-h+ox; 
        break; 
       case OOAnchorBottomLeft: 
        dx += -ay+oy; 
        dy += 0; 
        break; 
       case OOAnchorBottomRight: 
        dx += -ay+w-oy; 
        dy += ax-ox; 
        break; 
      } 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
      switch (_anchor) { 
       case OOAnchorTopLeft: 
        dx += ay-h-oy; 
        dy += -w; 
        break; 
       case OOAnchorTopRight: 
        dx += -w; 
        dy += -ax-w-ox; 
        break; 
       case OOAnchorBottomLeft: 
        dx += ay-h+oy; 
        dy += -ay-h; 
        break; 
       case OOAnchorBottomRight: 
        dx += ay-w-oy; 
        dy += -ax+w-ox; 
        break; 
      } 
      break; 
    } 

    CGFloat angle = 0; 
    switch (orientation) { 
     case UIInterfaceOrientationPortrait:   angle = 0;   break; 
     case UIInterfaceOrientationLandscapeRight:  angle = M_PI_2*3; break; 
     case UIInterfaceOrientationPortraitUpsideDown: angle = M_PI;  break; 
     case UIInterfaceOrientationLandscapeLeft:  angle = M_PI_2;  break; 
     case UIInterfaceOrientationUnknown:break; 
    } 

    [self setTransform:CGAffineTransformConcat(
     CGAffineTransformMakeTranslation(dx,dy), 
     CGAffineTransformMakeRotation(angle) 
    )]; 
} 

- (void) invoke { 
    [[Oovium controls] invoke:self]; 
} 

- (void) place { 
    CGSize sz = [UIScreen mainScreen].bounds.size; 
    CGPoint shift = shift_[self.anchor]; 
    CGFloat x = shift.x*(sz.width-self.size.width)+self.offset.x; 
    CGFloat y = shift.y*(sz.height-self.size.height)+self.offset.y; 
    self.frame = CGRectMake(x, y, self.size.width, self.size.height); 
} 

@end