CATransaction completion being called immediately

前端 未结 4 2160
盖世英雄少女心
盖世英雄少女心 2020-12-13 12:21

I\'m trying to execute a completion-block after my CAAnimation has finished. However, it seems that animation block is called before my animation completes. The animation st

相关标签:
4条回答
  • 2020-12-13 13:09

    You need to set the completion block before adding the animation.

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat: 1.0f] forKey:kCATransactionAnimationDuration];
    
    [CATransaction setCompletionBlock:^{
    // ... whatever you want to do when the animation is complete
    }];
    
    [self.googleMapsView animateToCameraPosition:[GMSCameraPosition 
                        cameraWithLatitude:LATITUDE
                                 longitude:LONGITUDE
                                      zoom:ZOOM]];
    
    [CATransaction commit];
    

    This must trigger the completion block after the completion of that animation on the view.

    0 讨论(0)
  • 2020-12-13 13:12

    Try to start the animation asynchronously:

    DispatchQueue.main.async {
        self.startAnimation()
    }
    

    because it can interfere with view drawing if you make some view setup before calling the animation.

    0 讨论(0)
  • 2020-12-13 13:13

    I'm not sure if this really is the correct fix, but by setting the completion-block before adding the animation for the layer, the completion-block is consistently called at the correct time.

    [CATransaction begin];
    [CATransaction setCompletionBlock:completionBlock];
    [self.view.layer addAnimation:self.dropAndBounceAnimation forKey:@"appearance"];
    [CATransaction commit];
    
    0 讨论(0)
  • 2020-12-13 13:19

    Here is Swift 3.0.1, Xcode 8 version:

    CATransaction.begin()
    
    CATransaction.setCompletionBlock({
      print("Transaction completed")
    })
    
    print("Transaction started")
    view.layer.add(dropAndBounceAnimation, forKey: "appearance")
    
    CATransaction.commit()
    
    0 讨论(0)
提交回复
热议问题