如何在iOS中动态绘制圆形、椭圆形、三角形等形状?

如何在iOS中动态绘制圆形、椭圆形、三角形等形状?,ios,objective-c,iphone,uiimage,core-graphics,Ios,Objective C,Iphone,Uiimage,Core Graphics,我有一个UICollectionView,可以显示圆形、椭圆形、三角形等形状。所以当我点击单元格时,我需要在UIView上绘制相同的形状,然后我需要移动并调整通过点击单元格绘制的视图的大小。它如何在iOS中实现?提前谢谢 关于贝塞尔路径的研究。这是可能的。希望它对您有用。您可以使用CAShapeLayer。画一个圆圈 shapeLayer = [CAShapeLayer layer]; shapeLayer.frame = aFrame; UIBezierPath *spath = [UIBez

我有一个UICollectionView,可以显示圆形、椭圆形、三角形等形状。所以当我点击单元格时,我需要在UIView上绘制相同的形状,然后我需要移动并调整通过点击单元格绘制的视图的大小。它如何在iOS中实现?提前谢谢

关于贝塞尔路径的研究。这是可能的。希望它对您有用。

您可以使用CAShapeLayer。画一个圆圈

shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = aFrame;
UIBezierPath *spath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:12 startAngle:0 endAngle:(2 * M_PI) clockwise:YES];
shapeLayer.strokeColor = [UIColor colorWithRed:71/255.0 green:157/255.0 blue:252/255.0 alpha:1].CGColor;
shapeLayer.path = spath.CGPath;
shapeLayer.strokeStart = 0;
shapeLayer.strokeEnd = 0;
shapeLayer.fillColor = nil;
shapeLayer.lineWidth = 2;
[self.layer addSublayer:shapeLayer];

我希望它对您有所帮助

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

怎么办

1。创建一个子类
UIView
并将其放置在可重用单元中

2。覆盖
drawRect(:)
方法,您将在其中完成所有绘图

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

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

您应该阅读CoreGraphics API参考:

还可以了解
上下文
drawRect:

圆的一个非常基本的例子是:

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()
}
重写func drawRect(rect:CGRect){
var path=UIBezierPath()
path.moveToPoint(CGPoint(x:,y:))
path.addLineToPoint(CGPoint(x:,y:))
point.closePath()
UIColor.redColor().set()
点笔划()
点填充()
}

您可以使用图层的属性:

  • 转弯半径
  • 边界宽度
  • 边界色

你的问题很好,但太难回答。你需要阅读CoreGraphics:)你看到这个
UICollectionView