2012-10-25 44 views
4

對於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 

它似乎沒有破壞任何東西。但是,當涉及錯誤的錯誤陳述時,我往往是一個完美主義者。我已經對這些錯誤進行了一些研究,但是我沒有發現任何適合這種情況的東西。

感謝所有幫助!再次,我會很感激任何和所有的幫助。

+1

請認真添加代碼,以便您的播放器在播放視頻時完成此操作:) –

+0

已編輯爲包含完成的影片代碼根據塞巴斯蒂安的要求 – Joseph

回答

1

問題的原因是故事板中使用了「Autolayout」。我不是100%確定爲什麼會在播放電影后導致問題,而不是之前。但是我固定它有兩種方法:

答:除自動佈局

B:與約束功能的發揮,直到它結束了工作。 (將UIImageView的高度更改爲0,並將Equals: Default更改爲可能的最低優先級。)

0

嘗試在moviePlayBackDidFinish方法中設置SetUserInteractionEnabledself和/或self.scrollviewYES。 此外,請檢查滾動視圖的ContentSize屬性的大小,以檢查大小是否大於您的實際屏幕大小,以查看是否導致滾動視圖的功能中斷。

+0

謝謝你的回覆。不幸的是,利用'self.scrollView.userInteractionEnabled = YES'並未解決問題。在插入一個斷點並檢查scrollView.contentSize變量後,它看起來在播放電影后大小適當。 – Joseph

2

你確定所有東西都從超視圖中刪除了嗎?另外,你可以嘗試用

MPMoviePlayerController *movieDone = (MPMoviewPlayerController *)[notification object]; 

,並更換

MPMoviePlayerController *movieDone = [notification object]; 

添加

movieDone = nil; 

爲了確保您的MPMoviePlayerController完全從上海華刪除,請按下一個按鈕,你的在視頻完成播放後查看(在添加MPMoviePlayerController之前創建)並查看是否觸發。導航控制器必須以模態方式呈現您的MPMoviewPlayerController或進行推送。如果以模式形式呈現,請在播放完成時嘗試解除播放。

+1

看到他的代碼他既沒有推動它,也沒有模態地添加它。這是滾動視圖的子視圖。所以我可能認爲MPMoviePlayer的視圖實際上仍然位於scrollview的頂部。就像Andrei說的那樣,檢查關閉視圖後,電影播放器​​框架後面的按鈕是否仍然響應。 – Craimasjien

+0

優秀的建議。我做出了你所建議的改變,Andrei,但是滾動仍然鎖定在視頻後。更有趣的是播放電影的按鈕在播放電影后仍然功能完好。 – Joseph

+0

我最終更改了movieplayer方法,並將其添加到原始帖子以反映更改。它或多或少地解決了原始問題,但出現了一組新的錯誤。 – Joseph

相關問題