【问题标题】:How does overlayViewTouched notification work in the MoviePlayer sample code在 MoviePlayer 示例代码中,overlayViewTouched 通知如何工作
【发布时间】:2010-02-05 03:51:08
【问题描述】:

我对苹果提供的MoviePlayer 示例代码有疑问。
我不明白overlayViewTouch 通知是如何工作的。当我触摸视图(不是按钮)时,我添加到其中的 NSlog 消息不会发送。

// post the "overlayViewTouch" notification and will send
// the overlayViewTouches: message
- (void)overlayViewTouches:(NSNotification *)notification
{
    NSLog(@"overlay view touched");
    // Handle touches to the overlay view (MyOverlayView) here... 
}

但是,如果我将它放在“MyOverlayView.m”中的 -(void)touchesBegan 中,我可以获得 NSlog 通知。这让我觉得它正在识别触摸但发送通知。

 // Handle any touches to the overlay view
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch* touch = [touches anyObject];
        if (touch.phase == UITouchPhaseBegan)
        {
            NSLog(@"overlay touched(from touchesBegan")
            // IMPORTANT:
            // Touches to the overlay view are being handled using
            // two different techniques as described here:
            //
            // 1. Touches to the overlay view (not in the button)
            //
            // On touches to the view we will post a notification
            // "overlayViewTouch". MyMovieViewController is registered 
            // as an observer for this notification, and the 
            // overlayViewTouches: method in MyMovieViewController
            // will be called. 
            //
            // 2. Touches to the button 
            //
            // Touches to the button in this same view will 
            // trigger the MyMovieViewController overlayViewButtonPress:
            // action method instead.

            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            [nc postNotificationName:OverlayViewTouchNotification object:nil];



  }    
}

谁能解释我错过了什么或做错了什么?

谢谢。

【问题讨论】:

    标签: iphone mpmovieplayercontroller movieplayer


    【解决方案1】:

    在我看来,示例代码缺少对通知的 addObserver 选择器调用。注册示例可以在 AppDelegate 中找到:

    [[NSNotificationCenter defaultCenter] addObserver:self 
                     selector:@selector(moviePreloadDidFinish:) 
                     name:MPMoviePlayerContentPreloadDidFinishNotification 
                     object:nil];
    

    在 NSNotificationCenter 文档中 当一个对象(称为通知发送者)发布通知时,它会向通知中心发送一个 NSNotification 对象。然后,通知中心通过向其发送指定的通知消息,将通知作为唯一参数传递,通知其通知满足注册时指定标准的任何观察者。

    如果没有观察者,NSNotificationCenter 将不会通知任何人。

    例如,只需在 init 中添加适当的寄存器。

    [[NSNotificationCenter defaultCenter] addObserver:self 
                     selector:@selector(overlayViewTouches:) 
                     name:OverlayViewTouchNotification 
                     object:nil];
    

    【讨论】:

      【解决方案2】:

      这是因为覆盖视图很小。您可以通过更改叠加视图的背景颜色来查看叠加视图所覆盖的区域。当您触摸该区域时,将发送通知。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        • 2011-02-06
        • 2017-03-06
        • 2017-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多