2012-01-29 2 views
0

버튼을 클릭하여 이러한 뷰를 화면에서 서로 밀어 내도록했습니다.오리엔테이션 변경 후 뷰가 잘못 슬라이드 됨

그러나 장치를 가로 방향으로 변경하면보기가 화면 밖으로 반쯤 만 튀어 나와 붙습니다. 왜 이런 일이 일어 났는지는 이해하지만 어떻게 고쳐야하는지 이해합니다.

버튼을 세로 및 세로 모드로 모두 클릭하면 화면이 완전히 슬라이드되지 않게 할 수 있습니까? 그게 내가해야 할 일이다.

내 코드는이

.H 파일

@interface AnimationBlocksViewController : UIViewController{ 
    IBOutlet UIView *theview; 
    IBOutlet UIView *theview2; 

    BOOL isAnimated; 
    BOOL switchback; 
} 

-(IBAction)animate:(id)sender; 
-(IBAction)change:(id)sender; 

@end 

하는 .m 파일 I 비슷한 질문이나 튜토리얼에 대한 피드백, 샘플, 또는 링크 감사

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    isAnimated = NO; 
} 

-(IBAction)animate:(id)sender;{ 
    if (isAnimated) { 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview setFrame:CGRectMake(0, 0, 320, 460)]; 
     [theview2 setFrame:CGRectMake(320, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     isAnimated=YES; 

    } 


    else{ 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview setFrame:CGRectMake(-320, 0, 320, 460)]; 
     [theview2 setFrame:CGRectMake(0, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     isAnimated=NO; 

    } 
} 

-(IBAction)change:(id)sender;{ 
    if (switchback) { 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview2 setFrame:CGRectMake(0, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     switchback=NO; 

    } 


    else{ 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.5]; 
     [theview2 setFrame:CGRectMake(320, 0, 320, 460)]; 
     [theview setFrame:CGRectMake(0, 0, 320, 460)]; 
     [UIView commitAnimations]; 
     switchback=NO; 

    } 
} 

처럼 읽습니다.

고맙습니다.

답변

0

보다는이다 이러한 상수 값에 의존하여 좌표를 theviewtheview2으로 설정하여보기 컨트롤러의 현재 경계를 기준으로이 동작을 단순화 할 수 있습니다. 즉 [theview2 setFrame:CGRectMake(self.view.bounds.size.width, 0, 320, 460)];. 그렇게하면 고정 된 크기를 유지하는 것에 의존 할 필요가 없습니다. 예를 들어 사용자가 통화 중일 때 앱을 실행하거나 나중에 상태 표시 줄을 숨기거나이보기를 배치하면 변경됩니다 컨트롤러 또는 내비게이션 또는 탭 표시 줄 컨트롤러).

우리가 완전히 뷰 컨트롤러의 뷰의 경계에 이러한 뷰의 크기를 기반으로 수 더 나아가 :

CGRect frameForView2 = self.view.bounds; 
frameForView2.origin.x = frameForView2.size.width; 
[theview2 setFrame:frameForView2]; 

당신이뿐만 아니라 그것은 부모보기의 완전 외부 뷰를 이동해야합니다뿐만 아니라 할 수있는 방법 부모 뷰의 너비와 높이를 부모 뷰의 너비와 높이와 같게 설정 했으므로 하위 뷰를 다시 이동할 때 하위 뷰가 부모를 채우는 지 확인하십시오.

+0

이상적인 해결책으로 "self.view.bounds.size.width"를 참조하십시오. 화면에서 왼쪽으로 이동하려면이 값을 음수로 할 수있는 방법이 있습니까? –

+0

아 잠깐, 그냥 사용 - = 이것은 완벽하게 당신에게 정말 고마워했습니다 !!! –

1

내 문제의 해결책을 발견했습니다. 세로 또는 가로 방향을 기준으로 너비와 높이 값을 변경하는 화면 방향에 연결된 변수를 만들었습니다. 이것은 작동하지만 아직 약간의 디버깅이 진행 중입니다. 더 나은 해결책이 있다면 알려주세요. 아래는 내 .H 내에 배치 될 필요가 추가 코드와하는 .m 파일

.H 코드

int scrnWdth; 
int scrnHght; 

@end

하는 .m 코드

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
     toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     scrnHght = 320; 
     scrnWdth = 480; 


    } 
    else 
    { 
     scrnHght = 480; 
     scrnWdth = 320; 
    } 
} 
관련 문제