gpt4 book ai didi

ios - 如何使 CGContextDrawPath 在嵌套 subview 上绘制?

转载 作者:行者123 更新时间:2023-11-29 11:55:53 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com