2

我想在设备旋转时提供自定义动画,完全覆盖默认动画。实现这一目标的最佳方法是什么?

顺便说一句,我计划的动画类型是:

a) 当设备进入横向时,从顶部滑出一个新的视图,就好像它正在下降一样。

b)当设备返回纵向时,该视图应向下滑动并消失。

4

1 回答 1

1

最佳是主观的,取决于您的整个应用程序。

处理轮换事件的一种相当直接的方法是告诉系统不要并自己处理它们。对于您想要的效果,即当设备旋转到侧面时,基本上相当于从侧面滑动(预旋转)视图,这似乎是合适的。

这是一个非常基本的示例,说明如何实现这种效果。

@implementation B_VCRot_ViewController // defined in .h as @interface B_VCRot_ViewController : UIViewController
@synthesize sideways; // defined in .h as @property (strong, nonatomic) IBOutlet UIView *sideways;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)orientationChange:(NSNotification *)note{
    UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
    CGSize sidewaysSize = self.sideways.frame.size;
    if (newOrientation == UIDeviceOrientationLandscapeLeft){
        [UIView animateWithDuration:1.0 animations:^{
            self.sideways.frame = CGRectMake(0, 0, sidewaysSize.width, sidewaysSize.height);
        }];
    }
    else {
        [UIView animateWithDuration:1.0 animations:^{
            self.sideways.frame = CGRectMake(self.view.bounds.size.width, 0, sidewaysSize.width, sidewaysSize.height);
        }];
    }
}
- (void)viewDidLoad{
    [super viewDidLoad];
    [self.view addSubview:sideways];
    self.sideways.transform = CGAffineTransformMakeRotation(M_PI_2); // Rotates the 'sideways' view 90deg to the right.
    CGSize sidewaysSize = self.sideways.frame.size;
    // Move 'sideways' offscreen to the right to be animated in on rotation.
    self.sideways.frame = CGRectMake(self.view.bounds.size.width, 0, sidewaysSize.width, sidewaysSize.height);
    // register for rotation notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    // Do any additional setup after loading the view, typically from a nib.
}
@end

我在这里所做的是在 中添加一个UIView横向的.xib并将其连接到一个IBOutlet命名的sideways. 在viewDidLoad我将它添加为子视图,预先旋转它,然后将其移出屏幕。我还将 self 添加为设备旋转通知的观察者(请记住稍后为该通知删除自己)。在shouldAutoRo..我指出这个 VC 只处理肖像。

当设备轮换NSNotificationCenter来电时orientationChange:。此时,如果设备向左旋转,我的sideways视图将从右侧滑入(看起来像是在向下滑动)。

显然,对于两个横向,代码会更复杂。此外,您可能不得不弄乱动画时间以使其感觉好像第二个视图正在“下降”

于 2012-02-07T21:16:37.467 回答