Tag: nstimer

如何停止使用dispatch_async创build的线程?

我想强行停止由dispatch_async创build的线程,如果它的使用时间过长,例如超过5分钟。 通过在互联网上search,我觉得有人认为没有办法阻止线程,有没有人知道? 在我的想象中,我想创build一个NSTimer来在指定的时间过后停止线程。 + (void)stopThread:(NSTimer*)timer { forcibly stop the thread??? } + (void)runScript:(NSString *)scriptFilePath { [NSTimer scheduledTimerWithTimeInterval:5*60 target:self selector:@selector(stopThread:) userInfo:nil repeats:NO]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [LuaBridge runLuaFile:scriptFilePath]; }); } 我的runLuaScript方法: + (void)runLuaFile:(NSString *)filePath { lua_State *L = luaL_newstate(); luaL_openlibs(L); int error2 = luaL_dofile(L, [filePath fileSystemRepresentation]); if (error2) { fprintf(stderr, "%s", lua_tostring(L, -1)); lua_pop(L, 1); } lua_close(L); […]

秒表计数2

我在Objective-C中制作一个秒表: – (void)stopwatch { NSInteger hourInt = [hourLabel.text intValue]; NSInteger minuteInt = [minuteLabel.text intValue]; NSInteger secondInt = [secondLabel.text intValue]; if (secondInt == 59) { secondInt = 0; if (minuteInt == 59) { minuteInt = 0; if (hourInt == 23) { hourInt = 0; } else { hourInt += 1; } } else { minuteInt += […]

iphone开发:如何使用NSTimer创buildanimation

在我的应用程序中,我在viewWillAppear中有这个代码。 它只是将从屏幕顶部到底部的随机对象animation化。 问题是我需要检测碰撞,到目前为止我所学到的是不可能在任何时候检索animation的坐标。 那我该如何使用NSTimer来实现呢? 或者我应该使用NSTimer? 我无法弄清楚。 任何线索将不胜感激。 -(void)viewWillAppear:(BOOL)animated { for (int i = 0; i < 100; i++) { p = arc4random_uniform(320)%4+1; CGRect startFrame = CGRectMake(p*50, -50, 50, 50); CGRect endFrame = CGRectMake(p*50, CGRectGetHeight(self.view.bounds) + 50, 50, 50); animatedView = [[UIView alloc] initWithFrame:startFrame]; animatedView.backgroundColor = [UIColor redColor]; [self.view addSubview:animatedView]; [UIView animateWithDuration:2.f delay:i * 0.5f options:UIViewAnimationCurveLinear […]

无法转换(定时器!) – >无效(((CFRunLoopTimer?) – >无效)! – 将NSTimer扩展转换为Swift 3

我试图将我在我的项目中使用的Pod转换为Swift 3.我没有写它,但原来的作者没有更新它,所以我分叉它,我试图自己做。 但… 我得到这个错误试图将扩展名转换为NSTimer到Swift 3: Cannot convert value of type '(Timer!) -> Void' to expected argument type '((CFRunLoopTimer?) -> Void)! 看起来,Swift 3处理程序types(Timer!) -> Void与旧的CFRunLoop风格的处理程序不兼容,但我不确定如何在保持与iOS 9的兼容性的同时将其转换。 我粘贴下面的代码,由Xcode转换。 您可以在https://github.com/entotsu/TKSubmitTransition/blob/master/SubmitTransition/Classes/NSTimerEx.swiftfind原始代码 干杯 import Foundation extension Timer { class func schedule(delay delay: TimeInterval, handler: (Timer!) -> Void) -> NSTimer { let fireDate = delay + CFAbsoluteTimeGetCurrent() let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, […]

NSTimer不能从AppDelegate重复

为什么在appDidFinishLaunching中不会重复呢? self.ti = [NSTimer timerWithTimeInterval:10. target:self selector:@selector(bounce:) userInfo:nil repeats:YES]; [self.ti fire]; 非常感谢 儒勒

如何将5秒添加到计时器

我试图做一个计时器显示以下 – 小时:分钟:秒:毫秒。 这是我的代码: var timer = NSTimer() var startTime = NSTimeInterval() func updateTime() { var currentTime = NSDate.timeIntervalSinceReferenceDate() var elapsedTime : NSTimeInterval = currentTime – startTime let hours = UInt8(elapsedTime / 3600.0) elapsedTime -= (NSTimeInterval(hours) * 3600) let minutes = UInt8(elapsedTime / 60.0) elapsedTime -= (NSTimeInterval(minutes) * 60) let seconds = UInt8(elapsedTime) elapsedTime -= […]

NSTimer在启动时会导致“无法识别的select器”崩溃

我正在使用NSTimer运行一个animation(现在只是称之为myMethod )。 但是,它造成了一个崩溃。 代码如下: @implementation SecondViewController // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. – (void) myMethod { NSLog(@"Mark Timer Fire"); } – (void)viewDidLoad { [super viewDidLoad]; NSLog(@"We've loaded scan"); [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(myMethod:) userInfo:nil repeats:YES]; animationTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(myMethod:) userInfo:nil repeats: YES]; } 这是崩溃期间的输出 […]

ios scheduledTimerWithTimeInterval的时间量

我想使用scheduledTimerWithTimeInterval做一定的时间周期性的任务可以说一个小时。 但我如何在我的代码上实现。 这是我的代码: timer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(doSomething:) userInfo: nil repeats: YES];

如何在UIView的不同位置给图像animation多个图像?

如何在'UIView'中的Different Locations给“图像”animationmultiple images ? 1)我有3个图像。 2)我想要在3 different locations添加这3 Imagesanimation,意味着在some duration animatedly添加一个个图像? 我怎样才能做到这一点?

在iOS 5上locking屏幕时,NSTimer事件不会发生

我已经看到了这几次,但我还没有真正看到一个明确的答案… 由于iOS 5我的应用程序似乎遇到了问题,如果用户locking屏幕,我的NSTimer在locking屏幕约20秒后停止发射。 这使得我的应用程序,如果用户locking屏幕,每次定时器触发无声点播放声音。 这只是因为我更新我的设备到iOS 5。 我的问题是1.是否有任何解决方法,使相同的function在更新的设备上工作或2.如果您的应用程序没有在locking状态下运行,build议您不要收到错误评论。