drawRect not being called in my subclass of UIImageView(在我的 UIImageView 的子类中没有调用 drawRect)
问题描述
我已将 UIImageView 子类化并尝试覆盖 drawRect 以便我可以使用 Quartz 2D 在图像上绘制.我知道这是一个愚蠢的新手问题,但我没有看到我做错了什么.界面如下:
I have subclassed UIImageView and tried to override drawRect so I could draw on top of the image using Quartz 2D. I know this is a dumb newbie question, but I'm not seeing what I did wrong. Here's the interface:
#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
上设置了一个断点,但它从未命中,这让我认为它根本不会被调用.视图首次加载时不应该调用它吗?我是否错误地覆盖了它?
I set a breakpoint on drawRect
and it never hits, leading me to think it never gets called at all. Isn't it supposed to be called when the view first loads? Have I incorrectly overridden it?
推荐答案
只有在视图可见且脏的情况下才会调用它.也许问题出在创建视图的代码中,或者在您的 Nib 中,如果您是这样创建它的?
It'll only get called if the view is visible, and dirty. Maybe the problem is in the code that creates the view, or in your Nib, if that's how you're creating it?
如果您尝试调试发布"版本,有时还会看到无法正确设置断点.
You'll also sometimes see breakpoints failing to get set properly if you're trying to debug a "Release" build.
我不知何故错过了您第一次将 UIImageView 子类化.来自文档:
I somehow missed the first time that you're subclassing UIImageView. From the docs:
特别注意事项
UIImageView 类被优化为将其图像绘制到显示器上.UIImageView 不会调用 drawRect: 在子类.如果您的子类需要自定义绘图代码,推荐你使用 UIView 作为基类.
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 将图像绘制到视图中是多么容易,因此可能没有充分的理由将 UIImageView 子类化.
So there you have it. Given how easy it is to draw an image into your view using [UIImage drawInRect:], or by using CALayer, there's probably no good reason to subclass UIImageView anyway.
这篇关于在我的 UIImageView 的子类中没有调用 drawRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在我的 UIImageView 的子类中没有调用 drawRect


- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01