我从Apple那里浏览了iBook,找不到任何定义:
有人可以解释结构dispatch_after吗?
dispatch_after(<#when: dispatch_time_t#>, <#queue: dispatch_queue_t?#>, <#block: dispatch_block_t?#>)
Run Code Online (Sandbox Code Playgroud) 我在Objective C中有一个应用程序,我正在转换到Swift.在Objective C中,我有这个方法:
[self.view performSelector:@selector(someSelector) withObject:self afterDelay:0.1f];
我正在使用Swift,我无法弄清楚如何做到这一点.我试过了:
self.view.performSelector(Selector("someSelector"), withObject: self, afterDelay: 0.1)
这是我得到的错误: 'performSelector' is unavailable: 'performSelector' methods are unavailable
我会用什么电话来调用方法afterDelay?
UPDATE
这是我最终得到的:
extension NSObject {
func callSelectorAsync(selector: Selector, object: AnyObject?, delay: NSTimeInterval) -> NSTimer {
let timer = NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: selector, userInfo: object, repeats: false)
return timer
}
func callSelector(selector: Selector, object: AnyObject?, delay: NSTimeInterval) {
let delay = delay * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
NSThread.detachNewThreadSelector(selector, toTarget:self, withObject: object) …Run Code Online (Sandbox Code Playgroud) 我怎样才能创建一个每两秒触发一次的计时器,它会在我屏幕上的HUD上将分数增加一个?这是我对HUD的代码:
@implementation MyScene
{
int counter;
BOOL updateLabel;
SKLabelNode *counterLabel;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
counter = 0;
updateLabel = false;
counterLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
counterLabel.name = @"myCounterLabel";
counterLabel.text = @"0";
counterLabel.fontSize = 20;
counterLabel.fontColor = [SKColor yellowColor];
counterLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
counterLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeBottom;
counterLabel.position = CGPointMake(50,50); // change x,y to location you want
counterLabel.zPosition = 900;
[self addChild: counterLabel];
}
}
Run Code Online (Sandbox Code Playgroud)