旋转Ipad时动画视图转换

时间:2011-03-17 17:44:42

标签: objective-c ios ipad rotation

您好,并提前感谢任何回复。

我有一个有两种不同视图的IPad应用程序:一个用于纵向,一个用于横向。 当我旋转设备时,视图之间的过渡不是很平滑。我想为两个视图之间的过渡设置动画,使其看起来更好。 这就是我的代码在轮换中的样子:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
        self.view = corePlotContent.view; //make graph the landscape view 
        self.view.transform = CGAffineTransformMakeRotation(deg2rad*(-360)); 
        self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
    }else{
        self.view = portraitView;
        self.view.transform = CGAffineTransformMakeRotation(0);
        self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);
    }
}

我想我需要覆盖“willAnimateRotationToInterfaceOrientation”方法并在那里添加一些动画,但我不知道如何编写代码。我之前没有动画,所以我没有取得任何进展。请有人帮帮我,谢谢!

2 个答案:

答案 0 :(得分:3)

方法:willRotateToInterfaceOrientation:duration:在用户界面开始旋转之前调用,它用于准备旋转视图 - 在过渡动画发生之前设置视图状态。

基本上,您不需要覆盖此方法,除非您要在视图过渡动画期间禁用视图交互,停止媒体播放或暂时关闭昂贵的绘图或实时更新。此方法未在动画块中调用

相反,您应该使用willAnimateRotationToInterfaceOrientation:duration:方法。此方法在动画块中调用,用于旋转视图 - 在此调用中进行的每个视觉属性更改都将正确设置动画。

希望这能解决你的问题。

答案 1 :(得分:1)

您可能不想手动修复图形的框架和坐标系,因为UIViewController实际上相应地旋转了它的视图。

当主视图的帧发生变化时,使用类似的东西自动调整大小:

corePlotContent.view.autoresizeMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

并使用图表视图中的layoutSubviews方法对图表进行任何本地更改。

缺乏性能可能来自旋转动画期间的大量绘制(在旋转期间每个帧重绘图形)。我建议你可以使用 willRotateToInterfaceOrientation 暂时重新禁用图表,并在调用 didRotateFromInterfaceOrientation 时重新启用它。

您也可以使用带有CPU Profiler仪器的仪器来检测代码中的瓶颈。

希望有所帮助。