UIScrollView breaks when adding a new view

I am new to coding the iOS platform and goal C in general. I am writing a simple iPhone application that uses a storyboard with a high png file displayed through UIImageView embedded in UIScrollView and next to it a button that will play the movie.

My problem is that when the movie ends / exits and returns to the original screen, scrolling in UIScrollView does not work. I nailed the "reason". This happens when I add an MPMoviePlayerViewController object.view object to a subview. But I'm not sure how to fix this problem. Here is my distilled code:

.h file

@interface StuffViewController : UIViewController @property (strong,nonatomic) IBOutlet UIImageView *imageView; @property (strong,nonatomic) IBOutlet UIScrollView *scrollView; -(IBAction) playMovie; -(void) moviePlayBackDidFinish:(NSNotification*)notification; @end 

.m file

 -(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]; } 

I identified the culprit by commenting on the sections, and, as I said, scrolling β€œblocks” in β€œ[[view] viewSubview: moviePlayer.view]; and doesn’t recover until I switch to another view and then return.

Any help on this would be greatly appreciated.

EDIT: I found an interesting wrinkle that can help detect the underlying problem.

I have been using MPMoviePlayerController all this time. However, when switching to MPMoviePlayerViewController , some interesting things happened.

Here is the modified - (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; } 

Interestingly, UIScrollView will still work, HOWEVER, if it was scrolled down, it would no longer be able to scroll up where it was when the movie was launched. I fixed this by adding self.scrollView.contentOffset = CGPointZero at the beginning of playMovie to tell scrollView to go to the beginning (so that there would be nothing above it to scroll back). I assume that adding some if statement to the code in viewDidAppear that would keep scrollView.contentSize from being re-executed could solve the problem of being unable to scroll through the backup, but I like the "cleanliness" of running it back to the top.

One last question. Using MPMoviePlayerViewController like this caused some interesting errors to appear in my debugger when the line MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; . They look like this:

 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 

It seems he did not break anything. However, I tend to be a perfectionist when it comes to erroneous allegations of errors. I did some research on these errors, but I did not find anything suitable that could have a strong hand in this situation.

Thanks for the help! Once again, I would appreciate any help with this.

+4
source share
3 answers

The cause of the problem was the use of "Autolayout" in the Storyboard. I am not 100% sure why this can cause problems after playing a movie, and not earlier. However, I fixed it:

A: Autolayout removal

or

B: play with restriction functions until it finishes work. (Changing the height of the UIImageView to 0 and changing Equals: Default to have the lowest priority.)

+1
source

Are you sure everything is removed from the supervisor? Alternatively, you can try replacing

  MPMoviePlayerController *movieDone = [notification object]; 

with

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

also add

 movieDone = nil; 

To make sure that your MPMoviePlayerController is completely removed from the supervisor, try clicking on the button on your view (created before adding MPMoviePlayerController) after the video finishes the game and see if it works. The navigation controller should present your MPMoviewPlayerController modulo or make a click. If it is modally represented, try rejecting it when playback ends.

+2
source

Try setting SetUserInteractionEnabled from self and / or self.scrollview to YES in the moviePlayBackDidFinish method. Also, check the size of the ContentSize scroll view ContentSize to see if your actual screen size is larger to see if the scroll view function breaks.

0
source

Source: https://habr.com/ru/post/1441985/


All Articles