开发者

How to animate rotation of MPMoviePlayerController?

开发者 https://www.devze.com 2023-02-23 07:38 出处:网络
I\'m making a video player for iPad and I\'m having some trouble animating the rotation properly. Usually I deal with rotation by setting the auto rotation mask, but since I\'m working with a video pl

I'm making a video player for iPad and I'm having some trouble animating the rotation properly. Usually I deal with rotation by setting the auto rotation mask, but since I'm working with a video player I want to preserve the aspect ratio and I'm not sure how I would do that with auto rotation mask.

I did a quick and dirty solution without animation just to get the correct behavior:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.75);
}

It works correctly, but it's not pretty. I'm currently preparing the app for a demo, so now working correctly isn't enough, it has to be pretty.

I tried the following and I think you can guess why it doesn't work:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrien开发者_StackOverflowtation duration:(NSTimeInterval)duration
{
    [UIView animateWithDuration:duration animations:^{
        moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.75);
    }];
}

That's right, self.view.frame hasn't been updated yet.

Any advice on how to deal with this, without hard coded frames?


There may be a cleaner way to do this, but I calculate what the new frame should be using the current one, and the new orientation:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  CGSize size = self.view.frame.size;
  CGRect newRect;
  if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    newRect = CGRectMake(0, 0, MIN(size.width, size.height), MAX(size.width, size.height));
  } else {
    newRect = CGRectMake(0, 0, MAX(size.width, size.height), MIN(size.width, size.height));
  }

  [UIView animateWithDuration:duration animations:^{
    player.view.frame = newRect;
  }];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消