2014-09-25 1 views
0

iPhone 카메라 앱에서 위쪽 및 아래쪽 막대와 같은보기를 만들려고합니다. 나는 탑 뷰와 하단 뷰를 세로로 유지할 수 없다.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, 장치 동작을주의 깊게 살펴보고 그렇게하면서 디버거를 살펴 보자. 어디서 잘못되었는지보십시오. –

관련 문제