我正在开发一个应用程序,其中菜单移开以显示第二个菜单。我正在使用NSTimer移动图像,并希望它在图像到达屏幕上的某个坐标时停止。但我目前使用的是不工作,它正在移动图像,但它并没有停止。我究竟做错了什么?编辑:这是Xcode 5
- (IBAction)showSubMenu:(id)sender {
moveTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveMenus) userInfo:nil repeats:YES];
[self stopMovement];
}
-(void)stopMovement {
if (mainMenu.center.y < -160) {
[moveTimer invalidate];
moveTimer = nil;
}
}
-(void)moveMenus {
mainMenu.center = (CGPointMake(mainMenu.center.x, mainMenu.center.y-1));
goButton.center = (CGPointMake(goButton.center.x, goButton.center.y-1));
Info.center = (CGPointMake(Info.center.x, Info.center.y-1));
helpButton.center = (CGPointMake(helpButton.center.x, helpButton.center.y-1));
subMenu.center = (CGPointMake(subMenu.center.x, subMenu.center.y-1));
description.center = (CGPointMake(description.center.x, description.center.y-1));
playEndless.center = (CGPointMake(playEndless.center.x, playEndless.center.y-1));
playQuick.center = (CGPointMake(playQuick.center.x, playQuick.center.y-1));
playSoundboard.center = (CGPointMake(playSoundboard.center.x, playSoundboard.center.y-1));
confirm.center = (CGPointMake(confirm.center.x, confirm.center.y-1));
}
提前感谢您的帮助! Rafee
答案 0 :(得分:1)
每次视图移动时都需要调用stopMovement
,因此请更改代码:
- (IBAction)showSubMenu:(id)sender
{
moveTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveMenus) userInfo:nil repeats:YES];
}
- (void)stopMovement
{
if (mainMenu.center.y < -160)
{
[moveTimer invalidate];
moveTimer = nil;
}
}
- (void)moveMenus
{
mainMenu.center = (CGPointMake(mainMenu.center.x, mainMenu.center.y-1));
goButton.center = (CGPointMake(goButton.center.x, goButton.center.y-1));
Info.center = (CGPointMake(Info.center.x, Info.center.y-1));
helpButton.center = (CGPointMake(helpButton.center.x, helpButton.center.y-1));
subMenu.center = (CGPointMake(subMenu.center.x, subMenu.center.y-1));
description.center = (CGPointMake(description.center.x, description.center.y-1));
playEndless.center = (CGPointMake(playEndless.center.x, playEndless.center.y-1));
playQuick.center = (CGPointMake(playQuick.center.x, playQuick.center.y-1));
playSoundboard.center = (CGPointMake(playSoundboard.center.x, playSoundboard.center.y-1));
confirm.center = (CGPointMake(confirm.center.x, confirm.center.y-1));
// Call here
[self stopMovement];
}