Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Styopnaa

macrumors newbie
Original poster
May 12, 2016
2
0
hello,i have a problem with card matching game,this is the button click method and i need a 0.5 second delay in the exact part.i use [NSThread sleepForTimeInterval:0.5] but it works wrong.It should turn both cards and after 0.5 sec if they are not the same it should turn back.And i can't use methods with selectors because they can't take any arguments.

Code:
- (IBAction)ClickedButtons: (id)sender

{

    count++;

    NSInteger index = [self.cardButtonsindexOfObject:sender];

    Card* card = cards[index];

    [sender setBackgroundColor:[UIColorwhiteColor]];

    [sender setTitle:[NSStringstringWithFormat: @"%@%@",card.rank,card.suit] forState:UIControlStateNormal];

    if(count > 1 && lastSender != sender)

    {

        count = 0;

        if([card.rankisEqualToString:lastRank] && [card.suitisEqualToString:lastSuit])

        {

            UIButton *theButton = (UIButton *)sender;

            UIButton *theLastButton = (UIButton *)lastSender;

            theButton.enabled = NO;

            theLastButton.enabled = NO;

            countForWin++;

            if(countForWin == 8)

            {

                UIAlertView *win = [[UIAlertViewalloc] initWithTitle: @"Congratulations"message: @"You Won"delegate:selfcancelButtonTitle: @"Ok"otherButtonTitles: nil];

                [win show];

            }

         

        }

        else

        {
            [NSThread sleepForTimeInterval:0.5] ;

            [sender setBackgroundColor:[UIColorgrayColor]];

            [lastSendersetBackgroundColor:[UIColorgrayColor]];

            [sender setTitle:[NSStringstringWithFormat: @""] forState:UIControlStateNormal];

            [lastSendersetTitle:[NSStringstringWithFormat: @""] forState:UIControlStateNormal];

        }


    }

    elseif(count > 1 && lastSender == sender)

    {

        flag = !flag;

        if(flag)

        {

            [sender setBackgroundColor:[UIColorgrayColor]];

            [sender setTitle:[NSStringstringWithFormat: @""] forState:UIControlStateNormal];

        }

        else

        {

            [sender setBackgroundColor:[UIColorwhiteColor]];

            [sender setTitle:[NSStringstringWithFormat: @"%@%@",card.rank,card.suit] forState:UIControlStateNormal];

        }

    }

    lastSender = sender;

    lastRank = card.rank;

    lastSuit = card.suit;

}
 
Last edited by a moderator:

AxoNeuron

macrumors 65816
Apr 22, 2012
1,251
855
The Left Coast
You shouldn't be pausing on the main thread. You should use dispatch_queue_create() and sleep in a background thread, then pop back on to the main thread when you are ready.
 

Styopnaa

macrumors newbie
Original poster
May 12, 2016
2
0
NSTimer needs selector for that but in my case i can't use any selectors
[doublepost=1463083104][/doublepost]Can you show me an example please?
 

firewood

macrumors G3
Jul 29, 2003
8,074
1,317
Silicon Valley
You can and should use a selector. You have to learn how to save any arguments you need somewhere, use an NSTimer for the delay, then pick up where you left off in the selector method (including recovering any needed arguments and/or model/state variables you need to continue).

A lot of beginners seem to have no clue how to save and restore the variables needed to exit a method and then continue where you left off later (hint: properties, dictionaries, model objects, global variables, and/or databases, etc.). This is a basic coding skill required to do event driven OOP.
 

dantastic

macrumors 6502a
Jan 21, 2011
572
678
Code:
    float seconds = 0.5;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // This block will be executed after a delay
    });
 

firewood

macrumors G3
Jul 29, 2003
8,074
1,317
Silicon Valley
But be careful. The code after or around a dispatch_after call will usually execute before the code inside the dispatch_after block (including loop repeat constructs). To make it (or a timer call) a pseudo-sequential construct, you have to follow it by immediately exiting the current method (without any looping back).
 

MarkCollette

macrumors 68000
Mar 6, 2003
1,559
36
Toronto, Canada
You might want to look at the animation functions in UIView that use blocks. Some can have a delay before starting the animation of flipping the card, and then in the completion handler you could flip them back or whatever.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.