是否有在时间更改时获取通知的最佳实践?
我使用的是NSTimer
每 0,002 秒检查一次时间是否更改(小时和分钟,而不是秒和毫秒),以保持应用程序中的标签每分钟与手机时钟同步。
我希望有更好的解决方案。
谢谢
我正在开发一款游戏,其中图像的变化率随着时间的推移而变化。起初图像每两秒改变一次,然后速度会加快。
var change = 2.0
func setupGame(){
change = 2.0
changeImage()
}
func changeImage(){
timer = NSTimer.scheduledTimerWithTimeInterval(change, target: self, selector: #selector(SecondViewController.changeColors), userInfo: nil, repeats: true)
change -= 0.1
}
Run Code Online (Sandbox Code Playgroud)
问题是它速度太快而且不停下来。最终变化值变为负值。
有办法让这个工作吗?
我在我的应用程序中有一个重复计时器的代码:
(在viewDidAppear内)
NSDate *date = [NSDate dateWithTimeIntervalSinceNow: 3];
NSTimer *restrictionTimer = [[NSTimer alloc] initWithFireDate: date
interval: 3
target: self
selector:@selector(changeRestriction)
userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
和
- (void) changeRestriction
{
NSLog(@"inside !!");
}
Run Code Online (Sandbox Code Playgroud)
但是,我没有看到任何输出,任何帮助?
我正在启动一个计时器,视图已加载
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager.delegate = self;
[NSTimer scheduledTimerWithTimeInterval:10.0 //这里的间隔必须是一个浮动
目标:自
选择器:@selector(onTick :)
userInfo:nil重复:是];
}
然后我有我的计时器方法:
- (void)onTick:(NSTimer*)timer {
[locationManager startUpdatingLocation];
}
然后调用位置管理器,因为它是一个委托:
- (void)locationManager:(CLLocationManager*)manager
didUpdateToLocation :( CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation {
NSLog(@"使其成为位置管理器");
NSTimeInterval timePassedSinceLastLocationUpdate = fabs([oldLocation.timestamp timeIntervalSinceNow]);
NSLog(@"更新时间:%f",timePassedSinceLastLocationUpdate);
的NSLog(@ "--------------");
//其他东西发生
[locationManager stopUpdatingLocation];
}
但是,显示的NSLOG是这样的:
2012-07-31 13:02:28.092 round5 [1713:f803]转发到位置经理
2012-07-31 13:02:28.095 round5 [1713:f803]更新时间:0.000000
2012-07-31 13:02: 28.095 round5 [1713:f803] --------------
2012-07-31 13:02:33.298 round5 [1713:f803]成为了位置经理
2012-07-31 13:02 :33.298 round5 [1713:f803]更新时间:5.222202
2012-07-31 13:02:33.300 round5 [1713:f803] --------------
2012-07-31 13:02 :44.086 round5 [1713:f803]转发到位置经理
2012-07-31 …
我有一个NSTimer
每10秒运行一次,并从LandingController.m开始.当您转到应用程序中的其他视图时,它将继续运行.我希望能够(当在该计时器内满足某个条件时)从另一个视图更新标签字段GuardMenu.m我想要更新的标签名为CurrentZone.text,我想将它从值"N"更新为值"Y".
这是我在LandingController.m上的计时器
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(checkForMessages)
userInfo:nil
repeats:YES];
Run Code Online (Sandbox Code Playgroud)
在LandingController.m上调用它
- (void)checkForMessages
{
if ( //some condition here ){
//update CurrentZone.text label in GuardMenu view and set equal to "Y"
} else {
//no need to update label in GuardMenu as it's currently equal to "N"
}
}
Run Code Online (Sandbox Code Playgroud) 我见过类似的多个问题; 但是,他们都没有解决我的问题.我的计时器太慢了.我只使用0.01
秒的间隔.这是我的代码:
@IBOutlet var timerLabel: UILabel!
var miliseconds = 0
var seconds = 0
func updateLabel() {
if miliseconds == 0 {
timerLabel.text = "\(seconds).00"
} else if miliseconds < 10 {
timerLabel.text = "\(seconds).0\(miliseconds)"
} else {
timerLabel.text = "\(seconds).\(miliseconds)"
}
}
var timer = NSTimer()
func updateTime() {
miliseconds++
if miliseconds == 100 {
miliseconds = 0
seconds++
}
updateLabel()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if timerState == 1 {
timer = …
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我在视图控制器上放置了一个标签,我希望该标签显示一个每毫秒递增1的数字.(我知道我可以每秒更新1000,但我希望它看起来很流畅)
@IBOutlet var label: UILabel!
var number = 0
var timer: NSTimer!
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("addNumber"), userInfo: nil, repeats: true)
}
func addNumber () {
number++
label.text = number.description
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
timer?.invalidate()
}
Run Code Online (Sandbox Code Playgroud)
代码对我来说都很合理,但是当我运行应用程序时,我发现这个数字每秒增加大约50,而不是每秒增加1000.这个错误远远不能接受!
令人惊讶的是,当我锁定屏幕几秒钟并再次解锁时,数字突然增加了几千!
我认为这是因为我在计时器触发时更新UI,因此需要更多时间.但我该如何解决这个问题呢?
我有这个计时器:
class InstallationViewController: BaseViewController {
var precentageTimer: Timer!
}
Run Code Online (Sandbox Code Playgroud)
在同一堂课中,我像这样启动计时器:
self.precentageTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { (timer) in
self.network.getClibrationPercentage(success: { (info, percent) in
self.isAnalysisCompleted(percent: percent)
self.precentageLbl.text = "\(percent)%"
self.instructionsLbl.text = info
self.precentageLbl.isHidden = false
}, failure: { (error) in
print(error)
})
})
Run Code Online (Sandbox Code Playgroud)
我也有这样的方法来解除任务:
func dissmissAllTasks() {
NotificationCenter.default.removeObserver(self)
self.setCalibrationEnableTimer?.invalidate()
self.setCalibrationEnableTimer = nil
self.snapShotTimer?.invalidate()
self.snapShotTimer = nil
self.precentageTimer?.invalidate()
self.precentageTimer = nil
}
Run Code Online (Sandbox Code Playgroud)
当我像这样单步执行 viewWillDisappear 时就会发生这种情况:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
dissmissAllTasks()
}
Run Code Online (Sandbox Code Playgroud)
但当我去其他 VC 时,我可以看到这个计时器一直在持续。我检查了调用的 dissmissAllTasks() ,我可以看到计时器为零。
为什么计时器一直走?
我正在制作一个社交媒体应用程序,并且注册屏幕中的一个功能是检查用户名是否实时拍摄并放置X或检查是否已采用(再次实时).我使用计时器来完成此操作但由于某种原因,计时器在几秒钟后停止.这是怎么回事?有没有更好的方法来做这件事?
override func viewDidLoad() {
username.delegate = self
email.delegate = self
password.delegate = self
var timer = NSTimer()
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("checkOrX"), userInfo: nil, repeats: true)
}
func checkOrX() {
var query = PFQuery(className: "_User")
query.whereKey("username", equalTo: self.username.text)
query.findObjectsInBackgroundWithBlock { (users, error) -> Void in
if let users = users {
self.usernameCheck.image = UIImage(named: "X.png")
} else {
self.usernameCheck.image = UIImage(named: "Check.png")
}
}
}
Run Code Online (Sandbox Code Playgroud) 如何设置每1秒更新一些信息的操作?
我正在创建聊天(使用社交网络API),我必须检查新消息.
我正在寻找一种方法为swift 2中的NSTimer()变量分配一个nil值.我将它用于秒表应用程序.
var timer = NSTimer()
@IBAction func stop(sender: AnyObject) {
timer.invalidate()
timer = nil
Run Code Online (Sandbox Code Playgroud)
我收到了错误
"无法指定Nil键入"NSTimer"
我想在声音播放完成后激活一个功能.我认为需要包含NSTimer,但我不确定如何实现它.如果你能给我一些有用的代码,那将会很有帮助.
nstimer ×12
ios ×8
swift ×7
objective-c ×3
time ×2
xcode ×2
audio ×1
clock ×1
cocoa ×1
iphone ×1
nstableview ×1
timer ×1
viewdidload ×1