0

我正在开发一个应用程序,其中菜单移开以显示第二个菜单。我正在使用 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));

}

我在这里先向您的帮助表示感谢!拉菲

4

1 回答 1

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];
}
于 2013-10-03T19:25:09.097 回答