UIView动画不适用于横向模式

时间:2012-12-04 09:36:09

标签: objective-c uiviewanimation

我正在使用UIView动画,其中第二个视图从底部出现并出现在视图的中心。但它不能在横向模式下工作,虽然我的应用程序支持横向模式,我实现了方法“willAnimateRotationToInterfaceOrientation:toInterfaceOrientation”。这是我的代码:

-(void) viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    self.view.backgroundColor = [UIColor blackColor];

    imageView= [[UIImageView alloc] initWithFrame:CGRectMake(0, 499, 320, 0)];
    imageView.image = [UIImage imageNamed:@"trans.png"];
    [imageView setClipsToBounds:YES];
    [imageView setContentMode:UIViewContentModeBottom];



    [self.view addSubview:imageView];


    [UIView animateWithDuration:1.0f
                          delay:0.5f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^(void) {
                         imageView.frame = CGRectMake(0, 92, 320, 320);

                     }
                     completion:NULL];


}


-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    CGRect screen = [[UIScreen mainScreen] bounds];
    float pos_y, pos_x;
    pos_y = UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]) ? screen.size.width/2  : screen.size.height/2;
    pos_x = UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]) ? screen.size.height/2 : screen.size.width/2;

    imageView.center = CGPointMake(pos_x, pos_y);

}

我想我在第二个视图的框架设置错误的地方......

1 个答案:

答案 0 :(得分:0)

您使用了willAnimateRotationToInterfaceOrientation,但必须使用didRotateFromInterfaceOrientation

willAnimateRotationToInterfaceOrientation会在旋转之前为您提供方向。

所以使用这个..........

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    imageView.center = self.view.center;
}

这肯定会帮助你......

有一个快乐的编码........................... - :)