Search code examples
iphoneiosxcodenstimeruiviewanimationtransition

Random the time interval. To blink an eye. Through UIView animateWithDuration:


I would like to create an animation to blink a eye, in a random interval. Just like a normal being that will blink it randomly.

However this is not what i wanted it to do, any thoughts ?

- (void)animateFrogEyeOpenClose
{   
    if (OpenEye) {
        [UIView animateWithDuration:0.1

                     animations:^{ 
                         eyeOpenImageView.alpha = 0.0;
                         eyeCloseImageView.alpha = 1.0;
                     } 
                     completion:^(BOOL  completed){
                         if (completed)
                             OpenEye = 0;
                             CloseEye = 1;
                             [self animateFrogEyeOpenClose];                  
                     }                      
         ];
    }
    else
    {
        [UIView animateWithDuration:0.1

                     animations:^{ 
                         eyeOpenImageView.alpha = 1.0;
                         eyeCloseImageView.alpha = 0.0;
                     } 
                     completion:^(BOOL  completed){
                         if (completed) 
                             OpenEye = 1;
                         CloseEye = 0;
                             [self animateFrogEyeOpenClose];                  
                     }                   
        ];
    }
}

Solution

  • Why don't you fire this method after a random time??? like

    //call this method for the first time 
     [self fireMethod:nil]; 
    //
    
    - (void)fireMethod:(id)sender{
        int rand = arc4random();  //set the random no acc. to your requirement
        [self animateFrogEyeOpenClose]; 
        [self  performSelector:@selector(fireMthod:) withObject:nil afterDelay:rand];
    }