Autosize 2 (恒屏和自动旋转)

自动旋转有如下几种实现方法:
1、使用自动调整属性进行旋转:

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

return YES;

}
该方法利用系统自动实现的旋转,view上的元素位置不会改变,可能有的元素会移到屏幕外。
2、在旋转时对视图进行重构(重新设置视图上各个元素的坐标位置)

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientationduration:(NSTimeInterval)duration {    

    UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;

    if (interfaceOrientation == UIInterfaceOrientationPortrait 

        || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)

    {

        button1.frame = CGRectMake(2020125125);

        button2.frame = CGRectMake(17520125125);

        button3.frame = CGRectMake(20168125125);

        button4.frame = CGRectMake(175168125125);

        button5.frame = CGRectMake(20315125125);

        button6.frame = CGRectMake(175315125125);

    }

    else 

    {

        button1.frame = CGRectMake(2020125125);

        button2.frame = CGRectMake(20155125125);

        button3.frame = CGRectMake(17720125125);

        button4.frame = CGRectMake(177155125125);

        button5.frame = CGRectMake(32820125125);

        button6.frame = CGRectMake(328155125125);

    }

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

 

Autosize 2 (恒屏和自动旋转)_第1张图片
 

 

有两个函数:

-(void) willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

该方法在发生在旋转动画完全完成之前调用。

-(void) willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientationduration:(NSTimeInterval)duration

此方法在旋转开始之后,最后的旋转动画发生之前调用。

3、切换视图

这种方法使用于比较复杂的界面,是需要分别设计横向模式和纵向模式,然后在使用的过程中自动切换。

InterfaceOrientation有四个属性:Portait, LandscapeLeft, LandscapeRight, PortaitUpsideDown.


 

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)

interfaceOrientation duration:(NSTimeInterval)duration {

    if (interfaceOrientation == UIInterfaceOrientationPortrait)

    {

        self.view = self.portrait;

        self.view.transform = CGAffineTransformIdentity;

        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));

        self.view.bounds = CGRectMake(0.00.0300.0480.0);

    }

    else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)

    {

        self.view = self.landscape;

        self.view.transform = CGAffineTransformIdentity;

        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));

        self.view.bounds = CGRectMake(0.00.0460.0320.0);

    }

    else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)

    {

        self.view = self.portrait;

        self.view.transform = CGAffineTransformIdentity;

        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));

        self.view.bounds = CGRectMake(0.00.0300.0480.0);

    }

    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)

    {

        self.view = self.landscape;

        self.view.transform = CGAffineTransformIdentity;

        self.view.transform = 

        CGAffineTransformMakeRotation(degreesToRadian(90));

        self.view.bounds = CGRectMake(0.00.0460.0320.0);

    }

}

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

     return YES;

 }

因为在上面的代码中使用到了Core Graphics框架,因此要把该框架连接到该项目中,具体的方法是:在Resources上面点右键Add->Existing Frameworks。

你可能感兴趣的:(Autosize 2 (恒屏和自动旋转))