【发布时间】:2012-03-31 14:06:46
【问题描述】:
我是 iOS 编程的初学者,如果我的问题是一个愚蠢的问题,请见谅。
我正在尝试制作一个在加载的图像上执行自定义绘图的应用程序。
为此,我发现一个解决方案是将UIView 子类化并编辑drawRect 方法。
我在下面的代码中实现了这一点,该代码在链接到 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 是我创建的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);
}
我遇到的问题是没有绘图,在NSLog 我有“INITIALIZING”消息,但没有“DRAWING”绘图。
所以基本上它生成了initWithFrame,但它不调用drawRect 方法。
你能指出我做错了什么吗?
【问题讨论】:
标签: ios objective-c uiview