我正在寻找类似于
的行为[[NSNotificationQueue defaultQueue] enqueueNotification:not postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName|NSNotificationCoalescingOnSender forModes:nil];
但不使用通知,以某种方式将选择器或块排队,而不是通知。
至于我的动机(只是为了看看这是否是合法的做法)。我在视图中添加了多个子视图,显然无法知道多少个子视图,所以每次添加子视图时,我都必须通过调用layoutIfNeeded
以特定方式布局子视图来执行一些计算。现在,我在想,如果我只能在runloop变为空闲时调用该方法(以某种方式推迟调用并合并它),那么在执行布局计算时,所有子视图都已经添加。希望它有意义。
-(void)layoutSubviews
{
[super layoutSubviews];
UIView* prevView = nil;
for (NSUInteger i=0; i<[self.subviews count]; i++) {
UIView* view = self.subviews[i];
CGFloat spacing = prevView!=nil?self.spacing:0;
view.topLeft = CGPointOffset(prevView.bottomLeft, spacing, 0);
prevView = view;
}
[self fitSubviews];
}
在我的layoutSubview
方法中添加了我的代码。
答案 0 :(得分:1)
对于一般问题,最简单的解决方案是:
- (void)setNeedsCustomTask
{
// cancel any previously scheduled call to perform the task
[NSObject
cancelPreviousPerformRequestsWithTarget:self
selector:@selector(doCustomTask)
object:nil];
// schedule a new call; because you've specified any delay
// at all this will be scheduled to occur in the future rather than
// right now and because you've specified a 0.0 delay it'll be
// as soon as possible in the future
[self performSelector:@selector(doCustomTask) withObject:nil afterDelay:0.0];
}
- (void)doCustomTask
{
NSLog(@"I am a custom task");
}
这是有效的,因为performSelector:withObject:afterDelay:
将来调度runloop上的调用。您也可以指定inModes:
,例如如果你想避免跟踪模式。