Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中使用UIBezierPath创建三角形_Ios_Swift_Uibezierpath_Cashapelayer - Fatal编程技术网

Ios 在Swift中使用UIBezierPath创建三角形

Ios 在Swift中使用UIBezierPath创建三角形,ios,swift,uibezierpath,cashapelayer,Ios,Swift,Uibezierpath,Cashapelayer,我正在尝试理解如何用Swift创建三角形。我发现这个代码创建了一个三角形 class TriangleLayer: CAShapeLayer { let innerPadding: CGFloat = 30.0 override init() { super.init() fillColor = Colors.red.CGColor strokeColor = Colors.red.CGColor lineWidth = 7.0 lineC

我正在尝试理解如何用Swift创建三角形。我发现这个代码创建了一个三角形

class TriangleLayer: CAShapeLayer {

  let innerPadding: CGFloat = 30.0  

  override init() {
    super.init()
    fillColor = Colors.red.CGColor
    strokeColor = Colors.red.CGColor
    lineWidth = 7.0
    lineCap = kCALineCapRound
    lineJoin = kCALineJoinRound
    path = trianglePathSmall.CGPath
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  var trianglePathSmall: UIBezierPath {
     let trianglePath = UIBezierPath()
     trianglePath.moveToPoint(CGPoint(x: 5.0 + innerPadding, y: 95.0))     // #1
     trianglePath.addLineToPoint(CGPoint(x: 50.0, y: 12.5 + innerPadding)) // #2
     trianglePath.addLineToPoint(CGPoint(x: 95.0 - innerPadding, y: 95.0)) // #3
     trianglePath.closePath()
     return trianglePath
 }
这个代码创建了一个这样的形状 在屏幕中部。

我试着调整和玩弄它,以了解它是如何工作的;然而,在这一点上,我意识到,我迷失了相当多的逻辑。我将上面三角形的CG点放在我头部的x-y轴上,看起来像:

 #1 x:35, y:95          #3 x:65, y:95
           #2 x:50, y: 42.5 
 #1 x:35, y:95          #3 x:65, y:95
           #2 x:50, y: 147.5 
但是如果我把点放在x-y轴上,三角形是颠倒的

我想要实现的是轴心国告诉我的,我想要实现

         .   .                                .
                 <like this.   not this> 
           .                                .   .

你只是把斧头倒过来。坐标系从0,0开始,在X方向向右延伸,在Y方向向下延伸

所以你的观点是:

           #2 x:50, y: 42.5 
 #1 x:35, y:95          #3 x:65, y:95
要获得所需的三角形,您需要:

 #1 x:35, y:95          #3 x:65, y:95
           #2 x:50, y: 42.5 
 #1 x:35, y:95          #3 x:65, y:95
           #2 x:50, y: 147.5 

你只是把斧头倒过来。坐标系从0,0开始,在X方向向右延伸,在Y方向向下延伸

所以你的观点是:

           #2 x:50, y: 42.5 
 #1 x:35, y:95          #3 x:65, y:95
要获得所需的三角形,您需要:

 #1 x:35, y:95          #3 x:65, y:95
           #2 x:50, y: 42.5 
 #1 x:35, y:95          #3 x:65, y:95
           #2 x:50, y: 147.5 
斯威夫特4*

使用AutoLayout执行此操作的最简单方法是:

打开故事板并在UIViewController中拖动UIView,将其定位,然后根据需要设置大小,这就是三角形的位置。将视图背景设置为透明。 创建一个新类,你可以随意命名我的名为TriangleView。这将是该课程的内容: 类TriangleView:UIView{

转到情节提要,选择UIView并在Identity Inspector中编写类名TriangleView

享受你的三角

斯威夫特4*

使用AutoLayout执行此操作的最简单方法是:

打开故事板并在UIViewController中拖动UIView,将其定位,然后根据需要设置大小,即三角形所在的位置。将视图背景设置为透明。 创建一个新类,您可以根据需要命名它。我将其命名为mine TriangleView。这将是该类的内容: 类TriangleView:UIView{

转到情节提要,选择UIView并在Identity Inspector中编写类名TriangleView

享受你的三角

swift5代码

//TriangleView

extension UIView {

    func setRightTriangle(targetView:UIView?){
        let heightWidth = targetView!.frame.size.width //you can use triangleView.frame.size.height
        let path = CGMutablePath()

        path.move(to: CGPoint(x: heightWidth/2, y: 0))
        path.addLine(to: CGPoint(x:heightWidth, y: heightWidth/2))
        path.addLine(to: CGPoint(x:heightWidth/2, y:heightWidth))
        path.addLine(to: CGPoint(x:heightWidth/2, y:0))

        let shape = CAShapeLayer()
        shape.path = path
        shape.fillColor = UIColor.blue.cgColor

        targetView!.layer.insertSublayer(shape, at: 0)
    }

    func setLeftTriangle(targetView:UIView?){
        let heightWidth = targetView!.frame.size.width
        let path = CGMutablePath()

        path.move(to: CGPoint(x: heightWidth/2, y: 0))
        path.addLine(to: CGPoint(x:0, y: heightWidth/2))
        path.addLine(to: CGPoint(x:heightWidth/2, y:heightWidth))
        path.addLine(to: CGPoint(x:heightWidth/2, y:0))

        let shape = CAShapeLayer()
        shape.path = path
        shape.fillColor = UIColor.blue.cgColor

        targetView!.layer.insertSublayer(shape, at: 0)
    }

    func setUpTriangle(targetView:UIView?){
     let heightWidth = targetView!.frame.size.width
        let path = CGMutablePath()

        path.move(to: CGPoint(x: 0, y: heightWidth))
        path.addLine(to: CGPoint(x:heightWidth/2, y: heightWidth/2))
        path.addLine(to: CGPoint(x:heightWidth, y:heightWidth))
        path.addLine(to: CGPoint(x:0, y:heightWidth))

        let shape = CAShapeLayer()
        shape.path = path
        shape.fillColor = UIColor.blue.cgColor

        targetView!.layer.insertSublayer(shape, at: 0)
    }

    func setDownTriangle(targetView:UIView?){
        let heightWidth = targetView!.frame.size.width
        let path = CGMutablePath()

        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x:heightWidth/2, y: heightWidth/2))
        path.addLine(to: CGPoint(x:heightWidth, y:0))
        path.addLine(to: CGPoint(x:0, y:0))

        let shape = CAShapeLayer()
        shape.path = path
        shape.fillColor = UIColor.blue.cgColor

        targetView!.layer.insertSublayer(shape, at: 0)
    }
}
swift5代码

//TriangleView

extension UIView {

    func setRightTriangle(targetView:UIView?){
        let heightWidth = targetView!.frame.size.width //you can use triangleView.frame.size.height
        let path = CGMutablePath()

        path.move(to: CGPoint(x: heightWidth/2, y: 0))
        path.addLine(to: CGPoint(x:heightWidth, y: heightWidth/2))
        path.addLine(to: CGPoint(x:heightWidth/2, y:heightWidth))
        path.addLine(to: CGPoint(x:heightWidth/2, y:0))

        let shape = CAShapeLayer()
        shape.path = path
        shape.fillColor = UIColor.blue.cgColor

        targetView!.layer.insertSublayer(shape, at: 0)
    }

    func setLeftTriangle(targetView:UIView?){
        let heightWidth = targetView!.frame.size.width
        let path = CGMutablePath()

        path.move(to: CGPoint(x: heightWidth/2, y: 0))
        path.addLine(to: CGPoint(x:0, y: heightWidth/2))
        path.addLine(to: CGPoint(x:heightWidth/2, y:heightWidth))
        path.addLine(to: CGPoint(x:heightWidth/2, y:0))

        let shape = CAShapeLayer()
        shape.path = path
        shape.fillColor = UIColor.blue.cgColor

        targetView!.layer.insertSublayer(shape, at: 0)
    }

    func setUpTriangle(targetView:UIView?){
     let heightWidth = targetView!.frame.size.width
        let path = CGMutablePath()

        path.move(to: CGPoint(x: 0, y: heightWidth))
        path.addLine(to: CGPoint(x:heightWidth/2, y: heightWidth/2))
        path.addLine(to: CGPoint(x:heightWidth, y:heightWidth))
        path.addLine(to: CGPoint(x:0, y:heightWidth))

        let shape = CAShapeLayer()
        shape.path = path
        shape.fillColor = UIColor.blue.cgColor

        targetView!.layer.insertSublayer(shape, at: 0)
    }

    func setDownTriangle(targetView:UIView?){
        let heightWidth = targetView!.frame.size.width
        let path = CGMutablePath()

        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x:heightWidth/2, y: heightWidth/2))
        path.addLine(to: CGPoint(x:heightWidth, y:0))
        path.addLine(to: CGPoint(x:0, y:0))

        let shape = CAShapeLayer()
        shape.path = path
        shape.fillColor = UIColor.blue.cgColor

        targetView!.layer.insertSublayer(shape, at: 0)
    }
}

这是苹果公司的二维绘图文档。0,0在左上角,Y轴向下递增。这是苹果公司的二维绘图文档。0,0在左上角,Y轴向下递增。