先插入CALayer
,然后再插入[CATransaction commit]
中的scrollViewDidScroll:
为什么它在scrollViewDidScroll:
中不能立即做出反应?
要解决快速拖动的问题,请在屏幕上空白。
可以慢速处理
代码如下:
- (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?层解决问题