How to make a static image appear after 3 seconds?

后端 未结 4 1043
后悔当初
后悔当初 2021-01-26 12:50

How would I make an image appear after 3 seconds?

相关标签:
4条回答
  • 2021-01-26 13:05

    I'm a big fan of using GCD (iOS 4+) because you can simplify your code with inline blocks.

    In your case, you should set the image to hidden in Interface Builder, then create an IBOutlet with a connection to an ivar in your class.

    Then you can simply run this in viewDidLoad or similar:

    dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 3.0); 
    dispatch_after(delay, dispatch_get_main_queue(), ^(void){
        yourImage.hidden = NO;
    }); 
    
    0 讨论(0)
  • 2021-01-26 13:06

    You can use:

    [self performSelector: withObject: afterDelay: ]
    
    0 讨论(0)
  • 2021-01-26 13:10

    This assumes that you are calling performSelector:withObject:afterDelay from the main thread, and that your UIImageView is initially hidden.

    //assumes theImageView.hidden = YES
    [self performSelector:@selector(showImage:) withObject:theImageView afterDelay:yourTimeInterval];
    
    -(void)showImage:(UIImageView*)anImageView {
        anImageView.hidden = NO;
    }
    

    It is important that performSelector is called from the main thread because the selector that is called after the delay will run on the same thread, and you do not want to update UI from anything other than the main thread as a general rule.

    0 讨论(0)
  • 2021-01-26 13:10

    I haven't used XCode in awhile, but I'll take a stab for ya..

    1. In your Interface Builder set the image's visibility as hidden

    2. When your app starts up, set some global variable to the current time in an init fxn

    3. In the main control loop for your UI, check if that global var contains a time that is more than 3 seconds ago, if so, change that image's visibility parameter to shown.

    Best I can really say without really taking a look, which isn't possible right now.

    Good luck!

    0 讨论(0)
提交回复
热议问题