0

在 iOS 应用程序开发方面,我是初学者。我想将一个标签从左向右移动,直到它达到屏幕宽度的一半——即标签应该移动 240 像素(标签像一个选取框一样从左向右移动)。

我使用了 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);
    }
}

请问我该如何解决?

4

3 回答 3

4

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

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;}];
于 2012-07-27T12:43:10.443 回答
1

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

[myTimer invalidate];
myTimer = nil;
于 2012-07-27T13:03:41.510 回答
1

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

你不能“暂停”一个定时器,所以一旦你这样做了,你就需要调用另一个定时器。

于 2012-07-27T12:42:37.010 回答