【发布时间】:2012-06-15 20:40:30
【问题描述】:
我有一个基于视图控制器的应用程序,其中包含我根据某些逻辑显示/隐藏的多个视图。我想绘制一个与 UIView 大小相同的矩形,以使其像框架/边框形状。
我在绘制矩形时遇到问题。我知道下面的代码应该这样做,但我不确定为什么没有调用或触发这个方法。我也没有在任何地方看到生成的 (void)drawRect:(CGRect)rect 方法,所以我自己放置了它。不知道我在这里缺少什么..
- (void)drawRect:(CGRect)rect;
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0); // yellow line
CGContextBeginPath(context);
CGContextMoveToPoint(context, 50.0, 50.0); //start point
CGContextAddLineToPoint(context, 250.0, 100.0);
CGContextAddLineToPoint(context, 250.0, 350.0);
CGContextAddLineToPoint(context, 50.0, 350.0); // end path
CGContextClosePath(context); // close path
CGContextSetLineWidth(context, 8.0); // this is set from now on until you explicitly change it
CGContextStrokePath(context); // do actual stroking
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5); // green color, half transparent
CGContextFillRect(context, CGRectMake(20.0, 250.0, 128.0, 128.0)); // a square at the bottom left-hand corner
}
【问题讨论】:
-
drawRect:是 UIView 上的一个方法,所以你是把这段代码放在视图控制器中还是继承了 UIView? -
@Brian,我把它放在了视图控制器中。 UIView 的子类是什么意思,目的是什么?谢谢。
标签: objective-c ios4 xcode4.2