gpt4 book ai didi

ios - 如何在 iOS 中动态绘制圆形、椭圆形、矩形等形状?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:55:00 29 4
gpt4 key购买 nike

我有一个 UICollectionView,可以显示圆形、椭圆形、矩形等形状。因此,当我单击 Cell 时,我需要在 UIView 上绘制相同的形状,然后我需要移动并调整通过单击 Cell 绘制的 View 的大小。它如何在 iOS 中实现?提前致谢。

最佳答案

如果您使用 UICollectionView,您的可重用单元格将具有可供绘制的“图层”。

怎么办?

1. 创建一个 UIView 子类并将其放入您的可重用单元格中。

2. 覆盖drawRect(_:) 方法,您将在里面完成所有绘图。

3.将您的形状/线条绘制代码添加到 drawRect 方法。

例如,将 UIBezierPath 类用于线、弧等。您将能够创建各种形状。

您应该阅读 CoreGraphics API 引用:

https://developer.apple.com/reference/coregraphics

还了解LayersContextsdrawRect:

的一个非常基本的例子:

class CircleView: UIView {
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {
return
}
context.addEllipse(in: rect)
context.setFillColor(.red.cgColor)
context.fillPath()
}
}

并使用 UIBezier 路径绘制线条(矩形、星形等):

override func drawRect(rect: CGRect) {
var path = UIBezierPath()
path.moveToPoint(CGPoint(x:<point x>, y:<point y>))
path.addLineToPoint(CGPoint(x:<next point x>, y:<next point y>))
point.closePath()

UIColor.redColor().set()
point.stroke()
point.fill()
}

关于ios - 如何在 iOS 中动态绘制圆形、椭圆形、矩形等形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43137821/

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