Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何停止重复计时器?_Objective C - Fatal编程技术网

Objective c 如何停止重复计时器?

Objective c 如何停止重复计时器?,objective-c,Objective C,我是iOS应用程序开发的初学者。我想将标签从左向右移动,直到它达到屏幕宽度的一半-即标签应移动240px(标签像字幕一样从左向右移动) 我使用了NSTimer,我想在标签达到视图宽度的一半时停止计时器 我使用了以下代码,但它将标签移出视图: - (void)viewDidLoad { [super viewDidLoad]; timer = [[NSTimer scheduledTimerWithTimeInterval:0.09 target:self selector:@se

我是iOS应用程序开发的初学者。我想将标签从左向右移动,直到它达到屏幕宽度的一半-即标签应移动240px(标签像字幕一样从左向右移动)

我使用了NSTimer,我想在标签达到视图宽度的一半时停止计时器

我使用了以下代码,但它将标签移出视图:

- (void)viewDidLoad {
    [super viewDidLoad];
    timer = [[NSTimer scheduledTimerWithTimeInterval:0.09 target:self selector:@selector(time:) userInfo:nil repeats:YES] retain];
}

- (void)time:(NSTimer *)theTimer {
    label.center = CGPointMake(label.center.x+3.5, label.center.y);

    NSLog(@"point:%@", label);

    if (label.center.x < - (label.bounds.size.width/2)) {
        label.center = CGPointMake(320+(label.bounds.size.width/2), label.center.y);
    }
}
-(void)viewDidLoad{
[超级视图下载];
计时器=[[NSTimer scheduledTimerWithTimeInterval:0.09目标:自选择器:@selector(time:)userInfo:nil repeats:YES]retain];
}
-(无效)时间:(n计时器*)计时器{
label.center=CGPointMake(label.center.x+3.5,label.center.y);
NSLog(@“点:%@”,标签);
if(label.center.x<-(label.bounds.size.width/2)){
label.center=CGPointMake(320+(label.bounds.size.width/2),label.center.y);
}
}

请问我如何解决这个问题?

要停止计时器,请执行
[timer invalidate]


您不能“暂停”计时器,因此一旦您这样做,您需要调用另一个计时器。

要停止计时器,请执行
[timer invalidate]


你不能“暂停”计时器,因此一旦你这样做,你需要调用另一个计时器。

如果你想停止重复计时器,你可以使用

if (/*You label is in position*/)
    [myTimer invalidate];
但这不是在iOS中制作动画的正常方式,请尝试以下方法:

CGRect endFrame = /*The frame of your label in end position*/
[UIView animateWithDuration:0.5 animations:^{ myLabel.frame = endFrame;}];

如果要停止重复计时器,可以使用

if (/*You label is in position*/)
    [myTimer invalidate];
但这不是在iOS中制作动画的正常方式,请尝试以下方法:

CGRect endFrame = /*The frame of your label in end position*/
[UIView animateWithDuration:0.5 animations:^{ myLabel.frame = endFrame;}];

使计时器无效的正确方法是

[myTimer invalidate];
myTimer = nil;

使计时器无效的正确方法是

[myTimer invalidate];
myTimer = nil;

这个任务对于动画来说是一个完美的工作。这将是一种方法,除非有什么原因你不能使用它们。你可以在下面的2个链接中看到我的答案。这个任务对于动画来说是一个完美的工作。这将是一种方法,除非有什么原因你不能使用它们。你可以在下面的2个链接中看到我的答案