我对苹果提供的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];
}
}
有人能说清楚我错过了什么或做错了吗?
谢谢。
答案 0 :(得分:0)
在我看来,示例代码缺少对Notification的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];
答案 1 :(得分:0)
这是因为叠加视图很小。您可以通过更改叠加视图的背景颜色来查看叠加视图覆盖的区域。当您触摸该区域时,将会发送通知。