NSNotification触摸覆盖

时间:2011-03-28 13:55:15

标签: objective-c mpmovieplayercontroller nsnotificationcenter

2个视图控制器,firstviewcontroller和MyOverlayView的4个文件。 MyOverlayView只接收触摸并发送应由firstviewcontroller接收的通知。我在overlayview上获得触摸事件但没有在firstviewcontroller上接收通知。有什么想法吗?

FVC报头

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import "MyOverlayView.h"

extern NSString *const OverlayViewTouchNotification;

@interface FirstViewController : UIViewController {
    MPMoviePlayerViewController *mvc;
    MyOverlayView *overlayView;
    NSArray *keyArray;
    NSMutableDictionary *urlDic;
}

@property (nonatomic, retain) MPMoviePlayerViewController *mvc;
@property (nonatomic, retain) IBOutlet MyOverlayView *overlayView;
@property (nonatomic, retain) NSArray *keyArray;
@property (nonatomic, retain) NSMutableDictionary *urlDic;

-(void)playMovieAtURL:(NSDictionary *)dic:(NSArray *)array:(int) rand;
-(void)overlayViewTouches:(NSNotification *)notification;
-(void)loadArray;
-(void)reset;
@end

FVC-实施

#import "FirstViewController.h"
#import "SMWebRequest.h"

NSString * const OverlayViewTouchNotification = @"overlayViewTouch";

@implementation FirstViewController
@synthesize mvc;
@synthesize overlayView;
@synthesize keyArray, urlDic;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewDidAppear:(BOOL)animated
{
    [self loadArray];
    if (!overlayView) {
        overlayView = [[MyOverlayView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    }
    NSArray *windows = [[UIApplication sharedApplication] windows];
    if ([windows count] >= 1)
    {
        // Locate the movie player window
        UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
        // Add our overlay view to the movie player's subviews so it is 
        // displayed above it.
        [moviePlayerWindow addSubview:self.overlayView];
    }

    //[self.parentViewController.view addSubview:overlayView];
}

-(void)loadArray{
    if (!urlDic) {
        NSMutableDictionary *tempDic = [NSMutableDictionary new];
        [tempDic setObject:@"mp4" forKey:@"euromount_high_res"];

        urlDic = [[NSDictionary alloc] initWithDictionary:tempDic];
    }

    if (!keyArray) {
        NSArray *tempArray = [urlDic allKeys];
        keyArray = [[NSArray alloc] initWithArray:tempArray];
    }
    //Random choice
    int point = rand() % ([keyArray count]);
    //Call play movie
    [self playMovieAtURL :urlDic :keyArray :point];
}

-(void)playMovieAtURL:(NSDictionary *)dic:(NSArray *)array:(int)rand
{   
    NSString *key = [array objectAtIndex:rand];
    NSString *path = [[NSBundle mainBundle] pathForResource:key ofType:[dic valueForKey:key]];


    if (mvc == nil) { mvc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]]; }

    mvc.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
    mvc.moviePlayer.shouldAutoplay = TRUE;
    mvc.moviePlayer.controlStyle = MPMovieControlStyleNone;

    // Register for the playback finished notification.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(myMovieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];

    [self presentModalViewController:mvc animated:YES];

    // Movie playback is asynchronous, so this method returns immediately. 
    [mvc.moviePlayer play];
} 

// When the movie is done,release the controller. 
-(void)myMovieFinishedCallback:(NSNotification*)aNotification 
{
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:MPMoviePlayerPlaybackDidFinishNotification 
                                                  object:nil]; 
    //for (UIView *view in self.view.subviews) {
    //    [view removeFromSuperview];
    //}
    mvc = nil;
    [self loadArray];
}

// Touches in the overlay view (not in the overlay button)
// post the "overlayViewTouch" notification and will send
// the overlayViewTouches: message
- (void)overlayViewTouches:(NSNotification *)notification
{
    NSLog(@"screen touched");
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload
{
    mvc = nil;
    keyArray = nil;
    urlDic = nil;
    [super viewDidUnload];
}

-(void)reset{
    for (UIView *view in self.view.subviews) {
        [view removeFromSuperview];
    }
    mvc = nil;
    keyArray = nil;
    urlDic = nil;
    [self loadArray];
}

- (void)dealloc
{
    [super dealloc];
}

@end

MyOverlayView报头

#import <UIKit/UIKit.h>


@interface MyOverlayView : UIView {
}

- (void)awakeFromNib;
- (void)dealloc;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

@end

MyOverlayView-实施

#import "MyOverlayView.h"
#import "FirstViewController.h"

@implementation MyOverlayView

// MPMoviePlayerController will play movies full-screen in 
// landscape mode, so we must rotate MyOverlayView 90 degrees and 
// translate it to the center of the screen so when it draws
// on top of the playing movie it will display in landscape 
// mode to match the movie player orientation.
//
- (void)awakeFromNib
{
    CGAffineTransform transform = self.transform;

    // Rotate the view 90 degrees. 
    transform = CGAffineTransformRotate(transform, (M_PI / 2.0));

    UIScreen *screen = [UIScreen mainScreen];
    // Translate the view to the center of the screen
    transform = CGAffineTransformTranslate(transform, 
        ((screen.bounds.size.height) - (self.bounds.size.height))/2, 
        0);
    self.transform = transform;

    CGRect newFrame = self.frame;
    newFrame.origin.x = 190;
    self.frame = newFrame;
}

- (void)dealloc {
    [super dealloc];
}

// Handle any touches to the overlay view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];
    if (touch.phase == UITouchPhaseBegan)
    {
        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc postNotificationName:OverlayViewTouchNotification object:nil];
    }    
}


@end

**** 编辑答案 * ** * *** 我没有为通知添加观察员。把它全部整理好了。

1 个答案:

答案 0 :(得分:0)

我没有为通知添加观察者。得到了所有排序