[iOS]学习笔记8 (iOS之阻塞)

我感觉,这个可以Win32里面的while( PeekMessage( ... ) )是一会儿事情。比较简单。

CFRunLoopRun();就完成了这个事情。

那么如何解除这个Block呢?

可以用这个:

CFRunLoopStop( someLoop );

someLoop可以通过CFRunLoopGetCurrent()函数来获得。

这样就可以轻易地等待各种线程了,比如说动画线程。

@interface UIViewDelegate : NSObject
{
	CFRunLoopRef     _currentLoop;
}
@end

@implementation UIViewDelegate
-(id) init:(CFRunLoopRef)runLoop 
{
	if (self = [super init]) 
              _currentLoop = runLoop;
	return self;
}

-(void) animationFinished: (id) sender
{
	CFRunLoopStop(_currentLoop);
}
@end

@implementation UIView (Block)
+ (void) commitAnimationsAndBlock
{
	CFRunLoopRef currentLoop = CFRunLoopGetCurrent();
	UIViewDelegate *delegate = [[UIViewDelegate alloc] init:currentLoop];
	[UIView setAnimationDelegate:delegate];
	[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
	[UIView commitAnimations];
	CFRunLoopRun();
	[delegate release];
}
@end

  

原文地址:https://www.cnblogs.com/healerkx/p/2327521.html