Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CALayer经常被重画,尽管它应该光栅化-->;为什么?_Ios_Objective C_Core Animation_Instruments - Fatal编程技术网

Ios CALayer经常被重画,尽管它应该光栅化-->;为什么?

Ios CALayer经常被重画,尽管它应该光栅化-->;为什么?,ios,objective-c,core-animation,instruments,Ios,Objective C,Core Animation,Instruments,我的应用程序中有一些相当复杂的层,我希望它们随着内容的变化而光栅化。这些图层所在的视图框架可以通过用手指拖动“间隔条”来更改。 我制作了一个简单的测试应用程序来可视化我的问题,并向您展示一些代码: @interface ViewController () { UIView* m_MainView; UIView* m_TopView; UIView* m_BottomView; UIView* m_MidView; int m_Position; } @e

我的应用程序中有一些相当复杂的层,我希望它们随着内容的变化而光栅化。这些图层所在的视图框架可以通过用手指拖动“间隔条”来更改。 我制作了一个简单的测试应用程序来可视化我的问题,并向您展示一些代码:

@interface ViewController () {
    UIView* m_MainView;
    UIView* m_TopView;
    UIView* m_BottomView;
    UIView* m_MidView;
    int m_Position;
}
@end

@implementation ViewController
- (void)loadView {
    m_MainView = [[UIView alloc] init];

    m_TopView = [[UIView alloc] init];
    m_TopView.backgroundColor = UIColor.blueColor;
    m_TopView.translatesAutoresizingMaskIntoConstraints = false;
    m_BottomView = [[UIView alloc] init];
    m_BottomView.backgroundColor = UIColor.blueColor;
    m_BottomView.translatesAutoresizingMaskIntoConstraints = false;
    m_MidView = [[UIView alloc] init];
    m_MidView.backgroundColor = UIColor.blackColor;
    m_MidView.translatesAutoresizingMaskIntoConstraints = false;
    [m_MainView addSubview:m_TopView];
    [m_MainView addSubview:m_BottomView];
    [m_MainView addSubview:m_MidView];

    CALayer* targetLayer = [[CALayer alloc] init];
    targetLayer.frame = CGRectMake (100, 200, 500, 50);
    targetLayer.backgroundColor = UIColor.yellowColor.CGColor;
    targetLayer.shouldRasterize = true;
    [m_BottomView.layer addSublayer:targetLayer];

    UIPanGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc]
        initWithTarget:self action:@selector(handlePan:)];
    [m_MainView addGestureRecognizer:recognizer];

    m_Position = 400;
    super.view = m_MainView;
}
- (void)handlePan:(UIPanGestureRecognizer*)sender {
    float touchY = [sender locationInView:m_MainView].y;
    m_Position = (int)touchY;
    [m_MainView setNeedsLayout];
}
- (void)viewWillLayoutSubviews {
    CGRect bounds = super.view.bounds;
    m_TopView.frame    = CGRectMake (0, 0, bounds.size.width, m_Position - 10);
    m_MidView.frame    = CGRectMake (0, m_Position - 10, bounds.size.width, 20);
    m_BottomView.frame = CGRectMake (0, m_Position + 10, bounds.size.width,
        bounds.size.height - m_Position - 10);
}
@end

现在。。。通过使用仪器和激活“颜色打绿色而不打红色”,可以看到图层何时被重画(然后以红色突出显示)。 在本例中,条形图(targetLayer)大部分时间是绿色的(缓存的),但如果我开始拖动间隔(这实际上改变了底部视图的框架),该层有时会变成红色。。。特别是当我反向拖动或缓慢拖动时。 在我的应用程序中,这会导致闪烁,因为重新绘制这些层的成本很高

为什么会发生这种情况?? 我从未更改图层的任何属性,但它无论如何都会被重新绘制? 我确信我遗漏了一些东西:-)

作为一种解决方法,我可以制作图层快照并使用图像。。。我认为这是可行的,如果没有解决方案,我将不得不使用这种方法,但我想了解这里的问题是什么

有人吗