将方向改回肖像

时间:2013-03-21 14:50:01

标签: iphone ios xcode4.6

使用我的应用时,用户可以点击并观看列表中显示的视频。一旦他们点击该视频,他们就可以将方向更改为纵向或横向。列表页面只是肖像。

我面临的错误是当用户观看横向视频并以横向退出页面时,我的列表页面变得混乱。

每当用户按下视频并返回列表时,我需要一种方法将方向转回画像。

在列表页面上我有

- (BOOL)shouldAutorotate{
  return YES;
}

我甚至尝试在viewDidAppear中调用shouldAutorotate方法,但这不起作用。我知道在页面加载后没有调用shouldAutorotate所以有没有办法检查方向然后翻转它或者只是让它成像而不管是什么?

我仍然需要风景,所以我不会将其从我的plist文件中删除。

任何帮助都会很棒。感谢

修改

以下是我如何调用我的视频播放器

    MPMoviePlayerViewController *player=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[[main_data objectAtIndex:indexPath.row] objectForKey:@"url"]]];

    UIView * playerView = [player view];
    [playerView setFrame: CGRectMake(0, 0, 480, 320)];

    CGAffineTransform landscapeTransform;
    landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
    landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);

    [playerView setTransform: landscapeTransform];

    [self presentMoviePlayerViewControllerAnimated:player];

    return;    

6 个答案:

答案 0 :(得分:2)

在iOS 6上,自转行为的机制发生了变化,所以如果你在iOS 6设备上进行测试,你需要做一些额外的工作。在视图控制器中,添加以下方法:

-(NSInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

答案 1 :(得分:1)

您可以使用多种方法。也许最简单和最可控的是MPMoviePlayerViewController的子类。

首先让 app 支持所有界面方向(在app-info.plist中)。然后使用iOS 6及更早版本操作系统的方法将列表视图(让我们假设为MyListViewController.m)限制为肖像:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 return UIInterfaceOrientationMaskPortrait;
}

- (NSUInteger) supportedInterfaceOrientations {
 return(UIInterfaceOrientationMaskPortrait);
}

- (BOOL) shouldAutorotate {
 return FALSE;   
}

现在创建一个派生自MPMoviePlayerViewController的新Objective C类,名为MyMoviePlayerViewController。这是MyMoviePlayerViewController.h

#import <MediaPlayer/MediaPlayer.h>

@interface MyMoviePlayerViewController : MPMoviePlayerViewController

@end

这里是MyMoviePlayerViewController.m

#import "MyMoviePlayerViewController.h"

@interface MyMoviePlayerViewController ()
@end

@implementation MyMoviePlayerViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 return YES;
}

- (NSUInteger) supportedInterfaceOrientations {
 return(UIInterfaceOrientationMaskAll);
}

- (BOOL) shouldAutorotate {
 return TRUE;    
}

-(id)initWithContentURL:(NSURL *)contentURL {
 UIGraphicsBeginImageContext(CGSizeMake(1,1));
 self = [super initWithContentURL:contentURL];
 UIGraphicsEndImageContext();
 if (self) {
    self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [[NSNotificationCenter defaultCenter] removeObserver:self
            name:MPMoviePlayerPlaybackDidFinishNotification
            object:[self moviePlayer]];
        [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(movieFinishedCallback:)
            name:MPMoviePlayerPlaybackDidFinishNotification
            object:[self moviePlayer]];
 }
 return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
[[self moviePlayer] setFullscreen:YES animated:NO];
[self moviePlayer].controlStyle = MPMovieControlStyleDefault;
}

- (void)movieFinishedCallback:(NSNotification*)notification {
    MPMoviePlayerController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
        name:MPMoviePlayerPlaybackDidFinishNotification
        object:moviePlayer];
    self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

然后,添加到MyListViewController.h

#import "MyMoviePlayerViewController.h"

并且,在MyListViewController.m中,使用:

MyMoviePlayerViewController *player =[[MyMoviePlayerViewController alloc]
    initWithContentURL:URLWithString:[[main_data objectAtIndex:indexPath.row]
    objectForKey:@"url"]]];
[self presentViewController:player animated:YES completion:nil];

显然,您可以调整设置(例如动画样式,显示控件......)以满足您的特定需求。

答案 2 :(得分:0)

隐藏视频控制器时如何调用[UIViewController attemptRotationToDeviceOrientation]

答案 3 :(得分:0)

在列表视图控制器

中实现此功能
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return UIInterfaceOrientationPortrait ;
}

-anoop

答案 4 :(得分:0)

试试这个,

在viewdidLoad方法中,写下此行以支持在app中的方向

//rotation
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];



#pragma mark -Rotation
//method for rotate your playerview, as user changes orientation
-(void)orientationChanged 
{
    NSLog(@"Orientation Changed..!!");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.35];
    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait)  {
        playerView.transform = CGAffineTransformMakeRotation(0);
    }
    else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
        playerView.transform = CGAffineTransformMakeRotation(M_PI);
    }
    else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)  {
        playerView.transform = CGAffineTransformMakeRotation(M_PI+(M_PI/2));
    }
   else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight)        {
        playerView.transform = CGAffineTransformMakeRotation(M_PI/2);
    }
    [UIView commitAnimations];
}

#ifdef IOS_OLDER_THAN_6
- (BOOL)shouldAutorotateToInterfaceOrientation:   (UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
#endif

// For Newer than IOS 6.
#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate
{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskPortrait);
}
#endif

希望这会有所帮助。祝你...编码快乐

答案 5 :(得分:0)

我认为您需要在- (void)movieFinishedCallback:(NSNotification*)notification

中的代码下面编写代码
- (void)movieFinishedCallback:(NSNotification*)notification 
{
    [self.view setFrame: CGRectMake(0, 0, 320, 480)];
    CGAffineTransform landscapeTransform;
    landscapeTransform = CGAffineTransformMakeRotation(270*M_PI/180.0f);
    landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
    [self.view setTransform: landscapeTransform];
}