iOS - completion block in UIView animateWithDuration gets called too early

后端 未结 3 1545
一生所求
一生所求 2020-12-15 03:56

I\'m trying to do some animation when a table view cell gets selected. For some reason, the completion block is getting called way too early. Even setting the duration to 10

3条回答
  •  爱一瞬间的悲伤
    2020-12-15 04:21

    Yes. It is being called too early because it's being interrupted somehow. Probably by a modal presentation transition or perhaps something else. Depending on your needs, the following may be a solution you like. We avoid the conflict by manually delaying the execution of our animation code like so:

    // To get this in Xcode very easily start typing, "dispatch_aft..."
    
    // Note the "0.2". This ensures the outstanding animation gets completed before we start ours
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:1.0 delay:0 options:0 animations:^{
            // Your animation code
        } completion:^(BOOL finished) {
            // Your completion code
        }];
    });
    

提交回复
热议问题