cha*_*rly 15 iphone objective-c drawrect
我想在viewContoller的视图中绘制填充矩形.我在viewDidLoad中写了下面的代码.但没有变化.怎么了?
CGRect rectangle = CGRectMake(0, 100, 320, 100);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
Run Code Online (Sandbox Code Playgroud)
Guy*_*aim 44
你不能在viewController中做到这一点.您需要扩展View并在"drawRect:"下添加代码
这将改变视图的绘图逻辑.
-(void) drawRect:(CGRect)rect{
[super drawRect:rect];
CGRect rectangle = CGRectMake(0, 100, 320, 100);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
}
Run Code Online (Sandbox Code Playgroud)
override func draw(_ rect: CGRect) {
let r = CGRect(x: 5, y: 5, width: 10, height: 10)
UIColor.yellow.set()
UIRectFill(r)
}
Run Code Online (Sandbox Code Playgroud)
就是这样。
| 归档时间: |
|
| 查看次数: |
32034 次 |
| 最近记录: |