Frage

This is my custom view for video player.

Interface File : Movieplayer.h

    #import <UIKit/UIKit.h>

    @interface MoviePlayer : UIView{

    }
    @property (strong) UIButton *videoButton;
    @end

Implementation File: Movieplayer.m

    #import "MoviePlayer.h"
    #import <MediaPlayer/MediaPlayer.h>

    @implementation MoviePlayer
    MPMoviePlayerViewController *movieController;

    - (id)initWithFrame:(CGRect)frame
    {
       self = [super initWithFrame:frame];
    if (self) {
    // Initialization code

    _videoButton = [[UIButton alloc] initWithFrame:self.frame];
    NSLog(@"My view frame: %@", NSStringFromCGRect(self.frame));
    [_videoButton setBackgroundImage:[UIImage imageNamed:@"video-default.jpg"] forState:UIControlStateNormal];
    [_videoButton addTarget:self action:@selector(showPlayer:) forControlEvents:UIControlEventTouchUpInside];
    int current_tag = rand();
    [_videoButton setTag:current_tag];
    NSLog(@"Current tag : %d",current_tag);
    [self addSubview:_videoButton];
}

return self;
    }

    -(void) showPlayer : (UIButton *) sender {
NSURL *movieURL = [NSURL URLWithString:@"http://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1211/sample_iTunes.mov"];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[movieController.view setFrame:_videoButton.frame];
[movieController.moviePlayer play];
[self addSubview:movieController.view];
    }


    @end

When i am using this class in viewcontroller, it works fully on the first control. But if i add 2 instance of the same class, the button selector is not fired for the second one.

here is how i am using it,

    - (void)viewDidLoad
    {
[super viewDidLoad];
UIScrollView *scrolview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];

// Do any additional setup after loading the view, typically from a nib.
MoviePlayer *mplayer = [[MoviePlayer alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
[scrolview addSubview:mplayer];

MoviePlayer *mplayer2 = [[MoviePlayer alloc] initWithFrame:CGRectMake(0, 180, 320, 180)];
[scrolview addSubview:mplayer2];

[self.view addSubview:scrolview];
[scrolview setContentSize:CGSizeMake(320, 600)];
    }

When i click on mplayer1, it plays the video, all works here. When i click on mplayer2 nothing happens. in mplayer2, it does not call Showplayer method.

Please help.

War es hilfreich?

Lösung

Change the frame of the button by below way.

_videoButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,frame.size.width, frame.size.height)];

For second MoviePlayer view, the frame starts with origin.y = 180. So your button will be placed outside of your view. Change the button frame as above to solve the issue.

Andere Tipps

The problem you're having is that the _videoButton is not actually inside the bounds of its parent MoviePlayer view. In the init function, you say:

_videoButton = [[UIButton alloc] initWithFrame:self.frame];

Since the _videoButton's frame is relative to its superview, this means for your section MoviePlayer, the _videoButton gets a frame with a y value of 180. This means it is 180 points below the top of the MoviePlayer. The MoviePlayer, though, is only 180 points tall, itself, so the _videoButton is entirely outside of its parent view. This prevents you from tapping on it, even though it's visible. (By default, views don't hide subviews if they are outside of their bounds.) You can verify this by giving your MoviePlayer view a background color.

The fix is easy: don't pass your own frame to any child views. You probably instead want to do something like:

_videoButton = [[UIButton alloc] initWithFrame:self.bounds];

If you want the child view to take up the entire parent view. (Or, even better, use a nib or autolayout constraints to avoid these kinds of issues.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top