【发布时间】:2012-10-25 07:06:17
【问题描述】:
总的来说,我对 iOS 平台和目标 C 的编码还很陌生。我正在使用情节提要编写一个简单的 iPhone 应用程序,该情节提要具有通过嵌入 UIScrollView 的 UIImageView 显示的高 png 文件以及旁边的一个按钮来播放电影。
我的问题是,当电影结束/退出并返回到原始屏幕时,UIScrollView 内的滚动不起作用。我已经确定了“原因”。当我将 MPMoviePlayerViewController object.view 添加到 self.view 子视图时会发生这种情况。但我不确定如何纠正这个问题。这是我的提炼代码:
.h 文件
@interface StuffViewController : UIViewController
@property (strong,nonatomic) IBOutlet UIImageView *imageView;
@property (strong,nonatomic) IBOutlet UIScrollView *scrollView;
-(IBAction) playMovie;
-(void) moviePlayBackDidFinish:(NSNotification*)notification;
@end
.m 文件
-(void) viewDidLoad {
self.imageView.image = [UIImage imageNamed:@"image.png"];
}
-(void) viewDidAppear:(BOOL)animated {
self.scrollView.contentSize = self.imageView.image.size;
}
-(void) playMovie {
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"movieTitle" ofType:@"mp4"]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
[[self view] addSubview:moviePlayer.view]; //SCROLLING BREAKS HERE
[moviePlayer setFullscreen:YES];
[moviePlayer play];
}
-(void)moviePlayBackDidFinish: (NSNotification*)notification {
MPMoviePlayerController *movieDone = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[movieDone.view removeFromSuperview];
[movieDone setFullscreen:NO];
}
我通过注释掉部分确定了罪魁祸首,就像我说的那样,滚动“锁定”在“[[self view] addSubview:moviePlayer.view];”在我导航到不同的视图然后返回之前不会恢复。
我们将不胜感激任何和所有的帮助。
编辑:我发现了一个有趣的皱纹,可能有助于发现根本问题。
我一直在使用MPMoviePlayerController。然而,在切换到MPMoviePlayerViewController 之后,发生了一些有趣的事情。
这里是改变的 -(void)playMovie
-(void) playMovie {
self.scrollView.contentOffset = CGPointZero;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:totalTitle ofType:@"mp4"]];
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playerController.moviePlayer play];
playerController = nil;
}
有趣的是 UIScrollView 仍然可以工作,但是,如果它已经向下滚动,它将不再能够从电影开始时的位置向上滚动。我通过在playMovie 的开头添加self.scrollView.contentOffset = CGPointZero 来解决此问题,以告诉scrollView 移动到顶部(因此它上面没有任何东西需要滚动回)。我假设在viewDidAppear 中的代码中添加某种 if 语句以防止 scrollView.contentSize 重新执行可能会解决无法向上滚动的问题,但是我喜欢它开始的“干净”回到顶部。
最后一期。像这样使用 MPMoviePlayerViewController 会导致在执行 MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 行时在我的调试器中弹出许多有趣的错误。它们如下:
Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextSaveGState: invalid context 0x0
Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextClipToRect: invalid context 0x0
Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextTranslateCTM: invalid context 0x0
Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextDrawShading: invalid context 0x0
Oct 25 10:25:51 Compy.local AppName[14590] <Error>: CGContextRestoreGState: invalid context 0x0
它似乎没有破坏任何东西。然而,当涉及到错误的错误陈述时,我往往是一个完美主义者。我已经对这些错误进行了一些研究,但是我还没有找到任何合适的方法来应对这种情况。
感谢到目前为止的所有帮助!再一次,我也将不胜感激。
【问题讨论】:
-
请在播放器完成视频播放时添加代码 :)
-
根据 Sebastian 的要求进行了编辑以包含完成的电影代码
标签: xcode uiscrollview subview