1、屏幕旋转

显示视图时,尽量不用单一的view去实现,将view放在viewcontroller里实现,这样可以在viewController中去添加其旋转方法,实现适配屏幕旋转。

(1)支持全部方向的旋转

iOS6.0需要下面三个方法,代码如下:

-(BOOL) shouldAutorotateToInterfaceOrientation : (UIInterfaceOrientation) toInterfaceOrientation{

    return (toInterfaceOrientation != UIInterfaceOrientationMaskPortraitUpsideDown);

}

- (BOOL)shouldAutorotate{

    return YES;

}

- (NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskAllButUpsideDown;

}

(2)禁止屏幕旋转

-  (BOOL) shouldAutorotateToInterfaceOrientation : (UIInterfaceOrientation) toInterfaceOrientation{

    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);

}

- (BOOL)shouldAutorotate{

    return NO;

}

- (NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)

}

2、屏幕旋转基本方法
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    NSLog(@"视图旋转之前自动调用"); 

- (void) willAnimateRotationToInterfaceOrientation : (UIInterfaceOrientation) toInterfaceOrientation duration : (NSTimeInterval) duration { 
    NSLog(@"视图旋转方向发生改变时会自动调用"); 

-(void) didRotateFromInterfaceOrientation : (UIInterfaceOrientation fromInterfaceOrientation{
    NSLog(@"视图旋转完成之后自动调用"); 

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSLog(@"视图旋转之前自动调用");
}
-(void) willAnimateRotationToInterfaceOrientation : (UIInterfaceOrientation) toInterfaceOrientation duration : (NSTimeInterval)duration{
    NSLog(@"视图旋转方向发生改变时会自动调用");
}

-(void) didRotateFromInterfaceOrientation : (UIInterfaceOrientation) fromInterfaceOrientation{
    NSLog(@"视图旋转完成之后自动调用");
}

3、旋转后调整视图大小。

当UIView设置成自动适配屏幕(即myView.autoresizesSubviews = YES)时,当我们重新设置myView的frame时(一般屏幕旋转时我们都会重新设置view的frame),会自动调用layoutSubviers方法,我们可以在该方法中判断屏幕的方向,并调整各子view的frame。

而设置UIView的属性autoresizingMask,可以调节其在父视图的相对位置:

UIViewAutoResizingFlexibleLeftMargin将调整view左侧到父视图的距离。

UIViewAutoResizingFlexibleTopMargin将调整view上侧到父视图的距离。

UIViewAutoResizingFlexibleWidth将调整view对应父视图的宽度。

其他的类似。

posted on 2013-07-16 10:20  nanoCramer  阅读(318)  评论(0编辑  收藏  举报