對於iOS平臺和目標C的編碼,我相當陌生。我正在編寫一個簡單的iPhone應用程序,使用故事板,其中有一個高大的PNG文件,通過嵌入在UIScrollView中的UIImageView顯示,旁邊有一個按鈕用於播放電影。添加新子視圖時UIScrollView突破
我的問題是,當電影完成/退出並返回到原始屏幕時,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
一些有趣的事情已經發生。
這裏是改變 - (空)的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
它似乎沒有破壞任何東西。但是,當涉及錯誤的錯誤陳述時,我往往是一個完美主義者。我已經對這些錯誤進行了一些研究,但是我沒有發現任何適合這種情況的東西。
感謝所有幫助!再次,我會很感激任何和所有的幫助。
請認真添加代碼,以便您的播放器在播放視頻時完成此操作:) –
已編輯爲包含完成的影片代碼根據塞巴斯蒂安的要求 – Joseph