为什么CALayer,[CATransaction commit]在scrollViewDidScroll中没有立即做出反应:

时间:2018-11-06 02:57:28

标签: ios objective-c calayer

先插入CALayer,然后再插入[CATransaction commit]中的scrollViewDidScroll:

为什么它在scrollViewDidScroll:中不能立即做出反应?

要解决快速拖动的问题,请在屏幕上空白。

q

可以慢速处理

img normal

代码如下:

- (void)viewDidLoad {
    self.backLayer = [[CALayer alloc] init];
    self.backLayer.frame = CGRectMake(0, 0, KScreenWidth, 0);
    [self.view.layer addSublayer: self.backLayer];
    self.backLayer.backgroundColor = navigationBarBuleColor.CGColor;
    self.backLayer.actions = @{@"position":[NSNull null]};
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat y = scrollView.contentOffset.y;
    if (y>0) {
        self.backLayer.opacity = 0;
    }
    else{

        [CATransaction begin];
        [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
        CGRect frame = self.backLayer.frame;
        frame.size.height = (fabs(y));
        self.backLayer.frame = frame;
        self.backLayer.opacity = 1;
        [CATransaction commit];
    }
}

我已经检查过Update CALayer sublayers immediately

我使用UIView,没关系。

代码如下:

- (void)viewDidLoad {
    self.assistView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0)];
    self.assistView.backgroundColor = navigationBarBuleColor;
    [self.view addSubview:self.assistView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat y = scrollView.contentOffset.y;
    if (y>0) {
        self.assistView.alpha = 0;
    } else{
        CGRect frame = self.assistView.frame;
        frame.size.height = (fabs(y));
        self.assistView.frame = frame;
        self.assistView.alpha = 1;
        [self.view setNeedsLayout];
    }
}

等于setNeedsLayout的CALayer方法是什么?

更多信息:我尝试通过How to set UITableView empty space's backgroundColor different?层解决问题

0 个答案:

没有答案