[英]UIView subclass: drawRect not called
I am a beginner in iOS programming, so sorry if my question is a stupid question.我是 iOS 编程的初学者,如果我的问题是一个愚蠢的问题,那么抱歉。
I am trying to make an app which performs custom drawing on a loaded image.我正在尝试制作一个对加载的图像执行自定义绘图的应用程序。
To do it, I found out that a solution is to subclass UIView
and edit the drawRect
method.为此,我发现一个解决方案是
UIView
并编辑drawRect
方法。
I made that on the following code which activates on an IBAction
linked to a button in the Interface Builder storyboard file.我在下面的代码中做了这个,它在链接到 Interface Builder 故事板文件中的按钮的
IBAction
上激活。
UIImageView *image = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"SampleImage.jpg"]];
image.frame = previewView.frame;
[image setContentMode:UIViewContentModeScaleAspectFit];
[previewView addSubview:image];
customView *aCustomView = [[customView alloc] initWithFrame: CGRectMake(image.bounds.origin.x, image.bounds.origin.y, image.bounds.size.width, image.bounds.size.height)];
[previewView addSubview:aCustomView];
customView
is the UIView
subclass that I created, whose init
and drawRect
methods are set like this: customView
是我创建的UIView
子类,其init
和drawRect
方法设置如下:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
NSLog(@"INITIALIZING");
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
NSLog(@"DRAWING");
CGContextClearRect(ctx, rect);
CGContextSetRGBFillColor(ctx, 0, 255, 0, 0.3);
CGContextFillEllipseInRect(ctx, rect);
CGContextSetRGBStrokeColor(ctx, 255, 0, 0, 1);
CGContextSetLineWidth(ctx, 2);
CGContextStrokeEllipseInRect(ctx, rect);
}
The problem that I have is that no drawing is made and on NSLog
I have the "INITIALIZING" message, but not the "DRAWING" drawing.我
NSLog
的问题是没有进行绘图,并且在NSLog
我有“正在初始化”消息,但没有“绘图”消息。
So basically it makes the initWithFrame
, but it doesn't call the drawRect
method.所以基本上它使
initWithFrame
,但它不调用drawRect
方法。
Could you please point me what am I doing wrong?你能指出我做错了什么吗?
Ref from the doc:参考文档:
The UIImageView class is optimized to draw its images to the display.
UIImageView 类经过优化,可以将其图像绘制到显示器上。 UIImageView will not call drawRect: a subclass.
UIImageView 不会调用 drawRect:一个子类。 If your subclass needs custom drawing code, it is recommended you use UIView as the base class.
如果您的子类需要自定义绘图代码,建议您使用 UIView 作为基类。
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html
Paolo,保罗,
Here are a few things you can try:您可以尝试以下几点:
initWithFrame
, check that the frame
variable contains what you would expect it to: NSLog(@"Frame: %@", NSStringFromCGRect(frame));
initWithFrame
,检查frame
变量是否包含您期望的内容: NSLog(@"Frame: %@", NSStringFromCGRect(frame));
[preview setNeedsDisplay];
[preview setNeedsDisplay];
after adding it to its superviewLastly, I would change最后,我会改变
image.frame = previewView.frame;
image.frame = previewView.frame;
with:和:
image.bounds = CGRectMake(0, 0, previewView.frame.size.width, previewView.frame.size.height);
image.bounds = CGRectMake(0, 0, previewView.frame.size.width, previewView.frame.size.height);
May be the question is aCustomView's frame is (0,0,0,0).可能问题是自定义视图的框架是 (0,0,0,0)。
You can try to pass a constant CGRect param, like this : CGRectMake(5, 5, 100, 100)。
你可以尝试传递一个常量 CGRect 参数,像这样:
CGRectMake(5, 5, 100, 100)。
The answer in my case was different from all the answers I've read for similar questions, and perhaps will help someone else.我的情况的答案与我为类似问题阅读的所有答案不同,也许会帮助其他人。 It turned out that on my Storyboard, "Inherit Module From Target" was unchecked, while at the same time the Module was None.
事实证明,在我的故事板上,“从目标继承模块”未选中,而同时模块为无。 I added the checkmark and the draw method was called the next time I ran the app.
我添加了复选标记,下次运行应用程序时会调用 draw 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.