我有以下代码:
timer = [[NSTimer scheduledTimerWithTimeInterval:0.50 target:self selector:@selector(onTimer) userInfo:nil repeats:YES] retain];
-(void) onTimer
{
}
Run Code Online (Sandbox Code Playgroud)
每0.50秒后OnTimer调用该方法.
但现在我想增加时间间隔.
这意味着:
OnTimer calls after 0.55
OnTimer calls after 0.60
OnTimer calls after 0.65
OnTimer calls after 0.70
OnTimer calls after 0.75
& so on.
Run Code Online (Sandbox Code Playgroud)
这有什么解决方案吗?我尝试了很多,但它不起作用.
我想知道是否有人可以解释为什么调度回主队列并创建重复NSTimer我不得不将它添加到RUN LOOP因为太火了?即使在使用时performselectorOnMainThread我仍然需要将它添加到RUN LOOP以使其触发.
以下是我的问题示例:
#define queue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define mainqueue dispatch_get_main_queue()
- (void)someMethodBeginCalled
{
dispatch_async(queue, ^{
int x = 0;
dispatch_async(mainqueue, ^(void){
if([_delegate respondsToSelector:@selector(complete:)])
[_delegate complete:nil];
});
});
}
- (void)compelete:(id)object
{
[self startTimer];
//[self performSelectorOnMainThread:@selector(startTimer) withObject:nil waitUntilDone:NO];
}
- (void)startTimer
{
NSTimer timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(callsomethingelse) userInfo:nil repeats:YES];
//NSDefaultRunLoopMode
[[NSRunLoop currentRunLoop] addTimer:_busTimer forMode:NSRunLoopCommonModes];
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我相信我措辞很差.如果我打电话,我想知道为什么 [[NSRunLoop currentRunLoop] addTimer:_busTimer forMode:NSRunLoopCommonModes];有必要.如果我不包括该行,则计时器不会触发.startTimersomeMethodBeginCalled
如果我呼吁startTimer来自viewDidLoad例如,我可以删除NSRunLoop线和计时器将每隔60秒射击.
我想知道为什么当你在GCD块中创建一个重复计时器时它不起作用?
这很好用:
-(void)viewDidLoad{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
}
-(void)runTimer{
NSLog(@"hi");
}
Run Code Online (Sandbox Code Playgroud)
但这项工作:
dispatch_queue_t myQueue;
-(void)viewDidLoad{
[super viewDidLoad];
myQueue = dispatch_queue_create("someDescription", NULL);
dispatch_async(myQueue, ^{
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
});
}
-(void)runTimer{
NSLog(@"hi");
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个"秒表"类型的功能.我有一个标签(显示已用时间)和两个按钮(启动和停止计时器).启动和停止按钮分别调用startTimer和stopTimer功能.计时器每秒触发并调用该increaseTimerCount函数.我也有一个ivar timerCount,它以秒为单位保持经过的时间.
- (void)increaseTimerCount
{
timerCountLabel.text = [NSString stringWithFormat:@"%d", timerCount++];
}
- (IBAction)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(increaseTimerCount) userInfo:nil repeats:YES];
}
- (IBAction)stopTimer
{
[timer invalidate];
[timer release];
}
Run Code Online (Sandbox Code Playgroud)
问题是按下开始按钮似乎有一个延迟(我假设这是由于每次调用startTimer时重新初始化计时器).有没有办法只是暂停和恢复计时器而不会使它失效并重新创建它?或更好/替代的方式这样做?
谢谢.
我根本不明白它,但NSTimer在我的应用程序肯定是在后台运行.我有一个NSLog由计时器运行的mehod,它在后台进行记录.它位于带有iOS 4.2.1的iPhone 4上.我在Info.plist中声明了位置背景支持.
我在这里和其他地方阅读了文档和许多讨论,这是不可能的.这是一个iOS错误吗?还是没有文档的功能?我不想使用它并在不久的将来发现,例如随着iOS 4.3的出现,Apple默默地"修复"它并且应用程序无法正常工作.
有人知道更多吗?
我是iOS编程的新手.我正在研究匹配游戏的文字.在这个游戏中我需要实现时间计数器,显示分钟和秒.我希望当我的游戏开始时我的计时器以3分钟开始.现在我想用秒来反向减少这个计时器.我的代码只工作了几秒钟.我的代码是:
secondsLeft--;
int seconds = (secondsLeft %3600) % 60;
Timerlbl.text = [NSString stringWithFormat:@"%02d",seconds];
if (seconds==0)
{
UIAlertView *pAlert = [[UIAlertView alloc]initWithTitle:@"Sorry!!"
message:@"TimeOver" delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:@"Cancel",nil];
[pAlert show];
[pAlert release];
}
}
Run Code Online (Sandbox Code Playgroud)
在Viewdidload中我通过计时器调用它..
countDown=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self
selector:@selector(TimeOver) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
请求任何人指导我如何在分钟和秒钟内完成它.
我现在有这个问题几天了,我不知道我做错了什么.
我的应用程序基本上只是创建一些计时器.我需要阻止他们并创造新的.但目前阻止他们是行不通的.
self.timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target:self, selector: "timerDidEnd:", userInfo: "Notification fired", repeats: false)
Run Code Online (Sandbox Code Playgroud)
那是我的计时器
func timerDidEnd(timer:NSTimer){
createUnrepeatedAlarmWithUpdateInterval()
}
Run Code Online (Sandbox Code Playgroud)
因为我的计时器不想停止我正在使用未重复的计时器并在它停止后自己启动它.
func stopAlarm() {
if self.timer != nil {
self.timer!.invalidate()
}
self.timer = nil
self.timer = NSTimer()
}
Run Code Online (Sandbox Code Playgroud)
这就是我停止计时器的方式.
alarmManager.stopAlarm()
alarmManager.createUnrepeatedAlarmWithUpdateInterval()
Run Code Online (Sandbox Code Playgroud)
我stopAlarm()在创建新计时器之前调用该函数.
我真的不知道我做错了什么,所以我很感激每一个答案:)
class AlarmManager: ViewController{
private var timer : NSTimer?
private var unrepeatedTimer : NSTimer?
private let notificationManager = NotificationManager()
private var current = NSThread()
private let settingsViewController = SettingsViewController()
func createRepeatedAlarmWithUpdateInterval(){
var timeInterval:NSTimeInterval = settingsViewController.getUpdateIntervalSettings()
if timer == …Run Code Online (Sandbox Code Playgroud) 我有以下UISearchbar代码:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString* endpoint =[NSString stringWithFormat:@"http://www.someurl/",
[searchText stringByReplacingOccurrencesOfString:@" " withString:@"+"]];
NSURL* url = [NSURL URLWithString:endpoint];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[myFetcher beginFetchWithDelegate:self didFinishSelector:@selector(searchResultsFetcher:finishedWithData:error:)];
}
Run Code Online (Sandbox Code Playgroud)
我希望在输入暂停后发送此请求,并在每次命中一个字符时重置计时器.我怎样才能做到这一点?
我在UIScrollView中放置了一个UIImageView,基本上这个UIImageView拥有非常大的地图,并在预定义的路径上用"箭头"指向导航方向创建动画.
但是,每当发生uiscrollevents时,我认为MainLoop冻结并且NSTimer没有被触发,并且动画停止了.
在UIScrollView,CAKeyFrameAnimation或NSTimer上是否存在解决此问题的现有属性?
//viewDidLoad
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(drawLines:) userInfo:nil repeats:YES];
- (void)drawLines:(NSTimer *)timer {
CALayer *arrow = [CALayer layer];
arrow.bounds = CGRectMake(0, 0, 5, 5);
arrow.position = CGPointMake(line.x1, line.y1);
arrow.contents = (id)([UIImage imageNamed:@"arrow.png"].CGImage);
[self.contentView.layer addSublayer:arrow];
CAKeyframeAnimation* animation = [CAKeyframeAnimation animation];
animation.path = path;
animation.duration = 1.0;
animation.rotationMode = kCAAnimationRotateAuto; // object auto rotates to follow the path
animation.repeatCount = 1;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.fillMode = kCAFillModeForwards;
[arrow addAnimation:animation forKey:@"position"];
}
Run Code Online (Sandbox Code Playgroud) 我有一个倒数计时器的应用程序.我已经使用一个标签更新了这个标签,这个标签是用定时器调用的函数更新的:
...
int timeCount = 300; // Time in seconds
...
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(actualizarTiempo:) userInfo:nil repeats:YES];
...
- (void)actualizaTiempo:(NSTimer *)timer {
timeCount -= 1;
if (timeCount <= 0) {
[timer invalidate];
} else {
[labelTime setText:[self formatTime:timeCount]];
}
}
Run Code Online (Sandbox Code Playgroud)
注意:formatTime是一个接收整数(秒数)并返回格式为mm:ss的NSString的函数
一切正常,也就是说,时间倒计时但问题是我在应用程序中有一个UITableView,如果我触摸桌面并拖动它(沿着单元格移动),计时器会停止,直到我从屏幕上松开手指...
这种行为是否正常?如果是,是否有任何方法可以避免它并在拖动表时使计时器工作?
nstimer ×10
ios ×7
objective-c ×7
iphone ×3
cocoa-touch ×2
nsrunloop ×2
background ×1
runloop ×1
swift ×1
uiscrollview ×1
uisearchbar ×1