如果您不想响应每个更改,我认为您需要使用某种计时方法。我认为这样的事情应该工作得很好。只要在 0.1 秒内再次调用该方法,第一行就会取消第二行。
-(void)splitViewDidResizeSubviews:(NSNotification *)notification {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(respondToSubviewChange) object:nil];
[self performSelector:@selector(respondToSubviewChange) withObject:nil afterDelay:.1];
}
-(void)respondToSubviewChange {
NSLog(@"here");
// Do your work here.
}
顺便说一句,如果此代码所在的类是拆分视图的委托,那么您无需注册此通知,它会自动注册。
编辑后:
我确实找到了另一种不使用任何计时机制的方法,但我不知道它有多强大。它依赖于 splitView:constrainMinCoordinate:ofSubviewAt: 当你在分隔器中使用 mouseDown 时调用的事实,然后再次使用 mouseUp。它在应用程序第一次启动时也被调用一次(或者可能在拆分视图所在的窗口、加载或其他什么时候——我没有用多个窗口测试它)。因此,将“timesCalled”设置为 -1(而不是 0)是为了让逻辑在应用程序启动时忽略第一次调用。此后,if 子句在对委托方法的所有其他调用(将在 mouseUp 上)评估为 true。
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex {
static int timesCalled = -1;
if (timesCalled % 2 == 1) {
NSLog(@"Do stuff");
// Do your work here.
}
timesCalled ++;
return 0 // 0 allows you to minimize the subview all the way to 0;
}