我是 Apple iOS 编程中 context
概念的新手。目前,我正在考虑 views
。我想用最简单的方法画一个圆弧,然后我找到了这个 question and answer .但是后来我对上下文概念感到困惑。我想画点东西,我需要调用绘图函数并在我希望绘图函数绘制的地方提供 view
。但是在那里,我对 UIGraphicsGetCurrentContext()
感到困惑。那是什么“当前上下文”?是 UIScreen.mainScreen()
吗?如果我想将“当前上下文”设置为 UICollectionViewCell
内的 View ,如何使绘制的所有像素都与 View 相关?我可以这样做吗?
最佳答案
在 CocoaTouch 中绘制的方式是继承 UIView
以创建自定义 View 并实现 drawRect:(CGRect)rect
方法。将绘图代码放在 drawRect
中。像这样的东西:
-(void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGPathRef path = CGPathCreateWithRect(rect, NULL);
[[UIColor blueColor] setStroke];
CGContextSetLineWidth(context, 4.0);
CGContextAddPath(context, path);
CGContextDrawPath(context, kCGPathFillStroke);
CGPathRelease(path);
}
如果需要,您可以将自定义 UIView 放在 UICollectionViewCell 中。
关于ios - 如何使 CGContextDrawPath 在嵌套 subview 上绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39136384/