Skip to content

通过CATransaction completion来结束CADisplayLink并不安全 #23

@sooYo

Description

@sooYo

####问题####
应用启动后,创建root tabbar controller后,VHLNavigation.m: pushNeedDisplay不断空转

####原因####
启动后创建root tabbar controller的自控制器中,有控制器包含了循环动画,导致以下代码执行失败(不会执行)

[CATransaction setCompletionBlock:^{
        [displayLink invalidate];
        displayLink = nil;
        vhlPushDisplayCount = 0;
        [viewController setPushToCurrentVCFinished:YES];
    }];

循环动画导致了CATransaction completion不会执行,从而最终导致空转

####复现####
目标controller

@implementation DisplayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    _testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.view addSubview:_testView];
    
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.fromValue = @(1);
    animation.toValue  = @(0);
    animation.repeatCount = CGFLOAT_MAX;
    animation.duration = 0.25;
    [_testView.layer addAnimation:animation forKey:@"test"];
}

发起点击controller,“completed”不会打印

@implementation ViewController

- (IBAction)test:(id)sender {
    DisplayViewController *vc = [[DisplayViewController alloc] init];
    
    [CATransaction setCompletionBlock:^{
        NSLog(@"completed");
    }];
    [CATransaction setAnimationDuration:10];
    [CATransaction begin];
    [self.navigationController pushViewController:vc animated:YES];
    [CATransaction commit];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions