setNeedsDisplay不适用于UIImageView的子类

时间:2022-07-14 21:09:02
import UIKit
class CustomView: UIView {
    override func drawRect(rect: CGRect) {
        print("Draw")
    }
}

Drag a UIView, set its class to be CustomView, create a reference customView to it.

拖动UIView,将其类设置为CustomView,为其创建引用customView。

When I call customView.setNeedsDisplay(), drawRect of CustomView get called.

当我调用customView.setNeedsDisplay()时,调用CustomView的drawRect。

import UIKit
class CustomImageView: UIImageView {
    override func drawRect(rect: CGRect) {
        print("DrawImage")
    }
}

Drag a UIImageView, set its class to be CustomImageView, create a reference customImageView to it.

拖动一个UIImageView,将其类设置为CustomImageView,为它创建一个引用customImageView。

When I call customImageView.setNeedsdisplay(), nothing happen. Why?

当我调用customImageView.setNeedsdisplay()时,什么都没发生。为什么?

1 个解决方案

#1


0  

The UIImageView class is optimized to draw its images to the display. UIImageView does not call the drawRect: method of its subclasses. If your subclass needs to include custom drawing code, you should subclass the UIView class instead.

UIImageView类经过优化,可将其图像绘制到显示器上。 UIImageView不调用其子类的drawRect:方法。如果您的子类需要包含自定义绘图代码,则应该替代UIView类。

#1


0  

The UIImageView class is optimized to draw its images to the display. UIImageView does not call the drawRect: method of its subclasses. If your subclass needs to include custom drawing code, you should subclass the UIView class instead.

UIImageView类经过优化,可将其图像绘制到显示器上。 UIImageView不调用其子类的drawRect:方法。如果您的子类需要包含自定义绘图代码,则应该替代UIView类。