Problem with making a delay in objective c

Discussion in 'iOS Programming' started by Styopnaa, May 12, 2016.

  1. Styopnaa, May 12, 2016
    Last edited by a moderator: May 12, 2016

    Styopnaa macrumors newbie

    Joined:
    May 12, 2016
    #1
    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;
    
    }
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I would suggest looking into the NSTimer class.
     
  3. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #3
    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.
     
  4. Styopnaa thread starter macrumors newbie

    Joined:
    May 12, 2016
    #4
    NSTimer needs selector for that but in my case i can't use any selectors
    --- Post Merged, May 12, 2016 ---
    Can you show me an example please?
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    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.
     
  6. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #6
    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
        });
     
  7. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #7
    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).
     
  8. MarkCollette macrumors 68000

    MarkCollette

    Joined:
    Mar 6, 2003
    Location:
    Toronto, Canada
    #8
    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.
     

Share This Page