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


基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01