How can I delay a method call for 1 second?

前端 未结 11 1375
别跟我提以往
别跟我提以往 2020-11-30 18:19

Is there an easy way delay a method call for 1 second?

I have a UIImageView that reacts on a touch event. When the touch is detected, some animations ha

11条回答
  •  一向
    一向 (楼主)
    2020-11-30 19:03

    You can also:

    [UIView animateWithDuration:1.0
                     animations:^{ self.view.alpha = 1.1; /* Some fake chages */ }
                     completion:^(BOOL finished)
    {
        NSLog(@"A second lapsed.");
    }];
    

    This case you have to fake some changes to some view to get the animation work. It is hacky indeed, but I love the block based stuff. Or wrap up @mcfedr answer below.


    waitFor(1.0, ^
    {
        NSLog(@"A second lapsed");
    });
    

    typedef void (^WaitCompletionBlock)();
    void waitFor(NSTimeInterval duration, WaitCompletionBlock completion)
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC),
                       dispatch_get_main_queue(), ^
        { completion(); });
    }
    

提交回复
热议问题