当涉及到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);
}
}
我该如何解决这个问题呢?
发布于 2012-07-27 12:43:10
如果您想停止重复计时器,您可以使用
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-27 12:42:37
要停止计时器,请执行[timer invalidate]
。
你不能“暂停”一个计时器,所以一旦你这样做了,你就需要调用另一个计时器。
发布于 2012-07-27 13:03:42
使计时器无效的正确方法是
[myTimer invalidate];
myTimer = nil;
https://stackoverflow.com/questions/11687882
复制相似问题