gpt4 book ai didi

ios - performSelector AfterDelay 在 Apple Watch 上不可靠

转载 作者:行者123 更新时间:2023-11-28 17:51:53 24 4
gpt4 key购买 nike

我有一个秒表类,它在其 tick 方法中定期更新 View 。该方法通过

触发
[self performSelector:@selector(tick) withObject:self afterDelay:0.1f];

这在 iPhone 上工作正常, View 大约更新一次。每十分之一秒。

现在我想使用相同的类在 Apple Watch 上实现这一点。当我启动秒表时, View 会得到更新。然而,2 秒后帧率下降,秒表变得无法使用。

下面是整个tick方法:

- (void)tick {
if (!self.running) return;
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval elapsed = currentTime - startTime;
if (countdown) {
curTime = originalTime - (elapsed * 1000);
}else {
curTime = originalTime + (elapsed * 1000);
curTime -= offset;
}

[self.delegate stopwatch:self tick:curTime];
long thisThousand = curTime / 1000;

if (curTime > 0 || !countdown) {
if (thisThousand != lastThousand) {
long seconds = curTime / 1000;
if (countdown) seconds++;
[self.delegate stopwatch:self fullSecond:seconds];
}
lastThousand = curTime / 1000;
[self performSelector:@selector(tick) withObject:self afterDelay:0.1f];
} else {
curTime = 0;
[self.delegate stopwatch:self fullSecond:0];
[self.delegate stopwatchStoped:self];
[self stopTimer];
}
}

关于如何让它发挥作用的任何想法。或者替代解决方案?

非常感谢!

编辑:UI 只是一个简单的标签,而不是花哨的动画。标签应该设置为从 0:00:00.1 到 0:00:00.2 等等。

最佳答案

如果您通过此方法更新 View (我假设 [self.delegate stopwatch:self fullSecond:seconds] 更新 View ?)如果是,那么您应该查看 CoreAnimation。动画设计得更好,可以处理随时间变化的 View (毕竟这就是动画!)。如果您正在为秒针制作动画,您可以创建一个使其“滴答作响”的动画,并无限期地重复该过程。还有一些委托(delegate)方法会在动画完成时通知您,您可以使用这些方法进行任何其他计算。

Look here有关 CoreAnimation 的文档。

performSelector:afterDelay: 的替代方法是使用 NSTimer:

// timer is an iVar
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(tick) userInfo:nil repeats:YES];

// then to stop the timer
[timer invalidate];

我首先建议使用 CoreAnimation。

关于ios - performSelector AfterDelay 在 Apple Watch 上不可靠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30637346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com