2014-09-25 2 views
0

Я пытаюсь сделать вид, как верхний и нижний столбцы приложения iPhone Camera. Я не могу получить вид сверху и снизу, чтобы оставаться в портрете.iOS - Сделать кнопки вращаться в виде неподвижных изображений, таких как приложение камеры

Когда я использую - (BOOL)shouldAutorotate, и, как ожидается, остановка вращения отключается. Я попытался обновить ограничения с setNeedsUpdateConstraints, но я все равно получаю эффект анимации. Я хочу, чтобы виды были заблокированы, а только UIButtons для вращения.

Как вы получаете только кнопки в режиме поворота и все остальное, чтобы оставаться заблокированными?

ответ

1

У меня есть UIViewController, который только поворачивает некоторые из его подзонов, когда устройство повернуто. (Это отлично работает под iOS7, но ломается под iOS8.) Вам нужно использовать CGAffineTransform для «ручного поворота» ваших просмотров.

Вот код:

@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 

Теперь это не работает отлично под iOS8, см UIView not resizing when rotated with a CGAffineTransform under iOS8 на мой вопрос.

+0

Спасибо! Это работает для первых нескольких поворотов, а затем представление начало перемещаться. Что может заставить его перестать работать? Я не могу понять это – Siriss

+0

Dunno, обратите внимание на движения устройства и пройдитесь через отладчик, делая это. Смотрите, где это происходит. –