- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我发现示例项目存在问题。我只是创建一个 scheduledTimer 来“激活”标签,然后,当我达到我想要的结果时,我使计时器无效并设置另一个计时器,这次作为“时钟”。这是我使用的代码
//
// ViewController.m
//
//
//
//
//
#import "ViewController.h"
#define ANIMATION_INTERVAL 0.07 // in secondi
#define ANIMATION_DURATION 1 // in secondi
@interface ViewController ()
{
int contatore;
NSString *hour;
NSString *minute;
NSString *second;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
contatore = 0;
[self startTimeAnimation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)startTimeAnimation
{
NSTimer * animationTimer = [NSTimer scheduledTimerWithTimeInterval:ANIMATION_INTERVAL target:self selector:@selector(timeout:) userInfo:nil repeats:YES];
}
-(void)timeout: (NSTimer *)timer
{
// Simulate of the Timer Duration with a counter
if (contatore < ceilf(ANIMATION_DURATION/ANIMATION_INTERVAL))
{
// Proceed with animation
contatore++;
int tempHour = arc4random_uniform(24);
int tempMinute = arc4random_uniform(60);
int tempSecond = arc4random_uniform(60);
if (tempHour < 10)
{
hour = [NSString stringWithFormat:@"0%d", tempHour];
}
else
{
hour = [NSString stringWithFormat:@"%d", tempHour];
}
if (tempMinute < 10)
{
minute = [NSString stringWithFormat:@"0%d", tempMinute];
}
else
{
minute = [NSString stringWithFormat:@"%d", tempMinute];
}
if (tempSecond < 10)
{
second = [NSString stringWithFormat:@"0%d", tempSecond];
}
else
{
second = [NSString stringWithFormat:@"%d", tempSecond];
}
_orarioLbl.text = [NSString stringWithFormat:@"%@:%@:%@", hour, minute, second];
}
else
{
// Stops animation
[timer invalidate];
[timer release];
contatore = 0;
[self setActualTime];
// Starts clock
NSTimer *clockTimer = [NSTimer timerWithTimeInterval:.5f target:self selector:@selector(updateClock) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:clockTimer forMode:NSRunLoopCommonModes];
}
}
-(void)updateClock
{
[self setActualTime];
}
-(void)setActualTime
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH"];
hour = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter setDateFormat:@"mm"];
minute = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter setDateFormat:@"ss"];
second = [dateFormatter stringFromDate:[NSDate date]];
_orarioLbl.text = [NSString stringWithFormat:@"%@:%@:%@", hour, minute, second];
[dateFormatter release];
}
@end
自从我启动“时钟”以来,内存仍然保持在 19/20 MB,并且持续分配。当计时器更新分钟值时,持久分配会增加,如您在 gif 中所见!这怎么可能?然而,19MB 的内存对于一个简单的时钟来说也太多了,不是吗?在 Instruments 上进行分析,新分配都是关于 CoreGraphics 的!
编辑 我在另一台设备上对其进行了测试,持久分配减少了。我不知道为什么,但我是这样解决的。谢谢
最佳答案
NSTimer
保留其目标,这可能会导致保留周期。获取对 self
的弱引用并将其设置为目标:
__weak typeof(self) weakSelf = self;
NSTimer * animationTimer = [NSTimer scheduledTimerWithTimeInterval:ANIMATION_INTERVAL target:weakSelf selector:@selector(timeout:) userInfo:nil repeats:YES];
和
__weak typeof(self) weakSelf = self;
NSTimer *clockTimer = [NSTimer timerWithTimeInterval:.5f target:weakSelf selector:@selector(updateClock) userInfo:nil repeats:YES];
我还可以建议您为 dateFormatter 创建一个属性,因为创建 NSDateFormatter
的成本很高。为什么要发布 dateFormatter?您没有使用 ARC 吗?
关于ios - 堆内存的持久化分配 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26733467/
注意:我在这个项目中使用 swift。 我目前正在开发我的第一个项目,如果在一段时间后没有按下按钮,我会尝试停止(无效)NSTimer。我找到了其他停止 NSTimer 的方法,但没有说明如何在一定时
我正在 Xcode4 中工作,在一个项目中,出于组织目的,我一直使用三个不同的 NSTimer,但我意识到,除了名称之外,计时器是相同的,因此很容易将所有三个计时器合并为一。对于我的项目来说,哪一个更
根据 Apple 文档,将同时使用 WKInterfaceTimer(Watch 本地,倒计时但在结束时不触发任何事件)和 NSTimer(在计时器结束时触发方法)。所以,我的 App Interfa
我目前正在尝试制作一个 queueHandler,它将一个对象数组作为输入,用于在一个简单的 Double 机器人上执行驱动命令。我目前正在尝试使用 GCD 以串行执行我的函数,但是当我在我的队列中使
有个问题...我有定时器 [NSTimer scheduledTimerWithTimeInterval:120
我创建了一个计时器, self.scanTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_SECONDS target:self s
我尝试使用以下代码停止 NSTimer: - (void)viewDidLoad { [super viewDidLoad]; timer3 = [NSTimer timerWithT
我正在制作一个带有计时器的应用程序。我从给定时间开始计算分钟和秒,直到 0。发生这种情况时,我启动一个警报 View 。 我的结构是这样的: 主线程方法分配一个新线程并初始化它。线程的入口点(方法)有
好吧,这段代码非常基本。用户将答案输入文本框,如果等于“第一+第二”,他们就得到一分。然后,他们有 5 秒钟的时间回答下一个数学问题。如果他们这样做了,函数“doCalculation”将再次运行,他
我正在创建益智游戏应用程序,并使用 NSTimer 显示时间(即 01:20)。当应用程序进入后台时,NSTimer 会暂停,但即使应用程序处于后台状态,我也想继续它。 例如,当应用程序进入后台时,计
我有一个带有 NSTimer *myTimer; 变量的类。在某些时候我会这样做: myTimer = [NSTimer scheduledTimerWithTimeInterval:20 targe
Edit2:为什么在“doSomething”方法中只更新了进度而不是point0? 编辑:使用我拥有的代码。我知道我一定忽略了一些东西,但我就是找不到它。 我正在编写一个 iPhone 应用程序,它
我的应用是一款体育应用。即使在后台游戏也应该继续,我为此使用了 nstimer。但我无法在后台运行计时器。每当应用程序处于后台时,计时器就会停止,返回时它会从离开时的同一时间继续。 最佳答案 您无法在
它将不断倒计时并更新当前时间。任何人都知道如何解决这个问题?我不明白 NStimer 如何识别这样的日期格式:20110803 23:59:59 最佳答案 查找NSDateFormatter 。您可以
是否可以在主线程上运行由 nstimer 调用的选择器? NSTimer 在它自己的线程中生成。 我的结构是一个线程调用一个带有 nstimer 的方法,nstimer 调用一个执行一些更新的方法,但
我在我的应用程序中使用了很多计时器。用于记录时间、移动物体、淡入淡出等。我在不同时间、同一 View 中使用同一个计时器来实现多个目的。我应该如何正确声明和无效或释放我的计时器? Atm 我这样声明计
我正在尝试使用 iPhone SDK 3.0 在线程上运行 NSTimer。我认为我做的一切都是正确的(新的运行循环等)。如果我在 viewDidDissappear 上调用 [timer inval
这是让我困惑了一段时间的事情。 我有一个 NSTimer,添加到 currentRunLoop 中,如果我不保留它,它就会崩溃。 NSTimer *timer = [[NSTimer timerWit
当我们使用 NSTimer 时,一旦在上述时间间隔后调用回调,UI 是否会被阻塞? 最佳答案 这要看情况。大多数时候,这不会成为问题。 如果,但是,满足以下两个条件,NSTimer 将阻塞 UI 线程
我使用大约 20 个 UIImageView 的小型飞机图像(50x50 像素)在 iPhone 屏幕上制作简单的动画。动画是通过按计时器间隔移动 UIImageView center 属性来完成的。
我是一名优秀的程序员,十分优秀!