xcode – UIScrollView在添加新子视图时中断
我很擅长编写iOS平台和目标C.我正在编写一个简单的iPhone应用程序,利用故事板,通过嵌入在UIScrollView中的UI
ImageView显示一个高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 它似乎没有破坏任何东西.然而,当谈到错误的错误陈述时,我倾向于成为完美主义者.我已经对这些错误进行过一些研究,但是我没有找到任何合适的东西可以在这种情况下发挥作用. 感谢目前为止所有的帮助!我再一次感谢任何和所有帮助. 解决方法
你确定从superview中删除了所有内容吗?此外,您可能会尝试替换
MPMoviePlayerController *movieDone = [notification object]; 同 MPMoviePlayerController *movieDone = (MPMoviewPlayerController *)[notification object]; 并且还添加 movieDone = nil; 要确保从superView中完全删除MPMoviePlayerController,请尝试在视频完成播放后按下视图上的按钮(在添加MPMoviePlayerController之前创建)并查看它是否会触发.导航控制器必须以模态方式呈现您的MPMoviewPlayerController或进行推送.如果是模态显示,请在播放结束时尝试将其解除. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |