Why an empty implementation of drawRect: will adversely affect performance during animation

前端 未结 2 1019
迷失自我
迷失自我 2021-02-06 05:08

I am subclassing my UIView class. The Xcode (I am using 4.6.3) auto generated code says,

/*
// Only override drawRect: if you perform custom drawing         


        
2条回答
  •  礼貌的吻别
    2021-02-06 05:22

    Only override draw() if you perform custom drawing.

    This means if we do not perform custom drawing, we should not write the overriding function. The "default" draw() is used. All graphics, if any, are handled "natively" so performance is best optimsed.

    An empty implementation adversely affects performance during animation.

    If we do override draw(), but we do not write any code in the function leaving this func empty, that is (1) an implementation of override draw() and it is also (2) an empty one as well. This will degrade performance. The following code

    public override func (_ r: CGRect) {
        // empty
    }
    

    is not efficient during animation. Persumably the system will attempt to redraw the whole canvas rather than redrawing only the affected part?

提交回复
热议问题