Pergunta

i am making a small iOS game that has many instances of a button on the same view. the view is filled with buttons using the image of a bubble.

i need to be able to change the image of one of these buttons when it is tapped and change it back after 5 seconds however i have absolutely no idea how to do it D:

could someone suggest a possible solution please?

Foi útil?

Solução

When your buttons are tapped, they will execute their action selector. The sender parameter passed to the action selector is in fact the button that was tapped and you can utilise this to change its properties. To reverse the change, you can use dispatch_after to delay the secondary change.

- (void)buttonWasTapped:(id)sender
{
    __block UIButton *button = (UIButton *)sender; // This is the button that was tapped
    [button setImage:[UIImage imageNamed:@"blah"] forState:UIControlStateNormal];

    double delayInSeconds = 5.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [button setImage:[UIImage imageNamed:@"original_image"] forState:UIControlStateNormal];
    });
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top