gpt4 book ai didi

ios - 基于CGPoint在屏幕上绘制UIView

转载 作者:行者123 更新时间:2023-12-01 16:25:58 25 4
gpt4 key购买 nike

我在屏幕上有四个随机位置的小圆圈。这些小圆圈可以在屏幕上的任何位置,并且位于 MapView 的前面。

enter image description here

如何根据这些圆圈创建形状?

enter image description here

此外,用户可以按一个按钮来随机更改圆圈的位置,因此必须重新创建形状。

enter image description here

到目前为止,我正在阅读有关 UIBezierPath 的信息,并且我能够基于点创建一个形状,但我无法动态地进行操作。

UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(52, 33)];
[bezierPath addLineToPoint: CGPointMake(145, 19)];
[bezierPath addLineToPoint: CGPointMake(158, 79)];
[bezierPath addLineToPoint: CGPointMake(66, 103)];
[UIColor.grayColor setFill];
[bezierPath fill];

最佳答案

您可以做的是添加 UIImageView使用透明背景颜色作为 MapView 的 subview ( ImageView 应该与您的 map View 具有相同的框架)。

viewDidLoad ,你可以开始一个 imageContext,使用:
UIGraphicsBeginImageContext(self.imageView.frame.size)
然后,您可以使用 CGMutablePath 在您的点之间画线。 :

func drawLines()
{
let point1 = CGPointMake(20, 100)
let point2 = CGPointMake(230, 150)
let point3 = CGPointMake(40, 340)

let points = [point1, point2, point3]

let context = UIGraphicsGetCurrentContext()
CGContextClearRect(context, self.imageView.frame)

let pathRef = CGPathCreateMutable();

for var i = 0; i < points.count; i++
{
let point = points[i]

if i == 0
{
CGPathMoveToPoint(pathRef, nil, point.x, point.y)
}
else
{
CGPathAddLineToPoint(pathRef, nil, point.x, point.y)
}
}

CGContextSetLineWidth(context, 5.0)
CGPathCloseSubpath(pathRef)
CGContextAddPath(context, pathRef);
CGContextSetStrokeColorWithColor(context, UIColor.brownColor().CGColor)
CGContextStrokePath(context)

let image = UIGraphicsGetImageFromCurrentImageContext()
self.imageView.image = image
}

当然,您应该根据您的需要调整此方法,但这会动态地工作。

这不是唯一的解决方案。如果你想更多地了解 Core Graphics,你应该阅读一些文档。它可能非常强大。

关于ios - 基于CGPoint在屏幕上绘制UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35385938/

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