我已将 UIImageView 子类化并尝试覆盖 drawRect 以便我可以使用 Quartz 2D 在图像上绘制。我知道这是一个愚蠢的新手问题,但我没有看到我做错了什么。界面如下:
#import <UIKit/UIKit.h>
@interface UIImageViewCustom : UIImageView {
}
- (void)drawRect:(CGRect)rect;
@end
以及实现:
#import "UIImageViewCustom.h"
@implementation UIImageViewCustom
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
}
return self;
}
- (void)drawRect:(CGRect)rect {
// do stuff
}
- (void)dealloc {
[super dealloc];
}
@end
我在 drawRect
上设置了一个断点,但它从未命中,这让我认为它根本不会被调用。 View 首次加载时不应该调用它吗?我是否错误地覆盖了它?
最佳答案
只有在 View 可见且脏的情况下才会调用它。也许问题出在创建 View 的代码中,或者在您的 Nib 中,如果您是这样创建它的?
如果您尝试调试“发布”版本,有时还会看到无法正确设置断点。
我不知何故错过了您第一次将 UIImageView 子类化。来自文档:
Special Considerations
The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: in a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.
所以你有它。鉴于使用 [UIImage drawInRect:] 或使用 CALayer 将图像绘制到 View 中是多么容易,因此可能没有充分的理由将 UIImageView 子类化。
关于iphone - 在我的 UIImageView 的子类中没有调用 drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2612152/