【问题标题】:Creating Triangle with UIBezierPath in Swift在 Swift 中使用 UIBezierPath 创建三角形
【发布时间】:2015-12-22 12:32:03
【问题描述】:

我正在尝试了解如何使用 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
 }

这段代码创建了一个像这样的形状 在屏幕中间。

我尝试对其进行调整和尝试以了解它是如何工作的;但是,在这一点上,我意识到我对逻辑迷失了很多。我将上面三角形的 CGPoints 放在我头上的 x-y 轴上,看起来像:

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

但是如果我将点放在 x-y 轴上,三角形就会倒置。

我想要实现的就是轴所告诉的,我想要实现..

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

【问题讨论】:

标签: ios swift uibezierpath cashapelayer


【解决方案1】:

你只是把头上的斧头倒过来了。坐标系从 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: 147.5 

【讨论】:

    【解决方案2】:

    Result triangles

    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)
        }
    }
    

    【讨论】:

      【解决方案3】:

      Swift 4.*

      使用 AutoLayout 最简单的方法:

      1. 打开您的故事板,将UIView 拖入UIViewController,定位它并根据需要设置大小(这就是三角形所在的位置)。将视图背景设置为透明。
      2. 创建一个新类,你可以随意命名它(我命名为我的TriangleView)。这将是该课程的内容:

      class TriangleView: UIView {

      //    predefined variables that can be changed
          var startPoint: CGPoint = CGPoint(x: 0.0, y: 0.5)
          var endPoint: CGPoint = CGPoint(x: 1.0, y: 0.5)
          var firstGradientColor: UIColor = UIColor.white
          var secondGradientColor: UIColor = UIColor.blue
      
          let gradient: CAGradientLayer = CAGradientLayer()
      
          override func draw(_ rect: CGRect) {
              let height = self.layer.frame.size.height
              let width = self.layer.frame.size.width
      
      //        draw the triangle 
              let path = UIBezierPath()
              path.move(to: CGPoint(x: width / 2, y: 0))
              path.addLine(to: CGPoint(x: width, y: height))
              path.addLine(to: CGPoint(x: 0, y: height))
              path.close()
      
      //        draw the triangle 'upside down'
      //        let path = UIBezierPath()
      //        path.move(to: CGPoint(x: 0, y: 0))
      //        path.addLine(to: CGPoint(x: width, y: 0))
      //        path.addLine(to: CGPoint(x: width / 2, y: height))
      //        path.close()
      
      //        add path to layer
              let shapeLayer = CAShapeLayer()
              shapeLayer.path = path.cgPath
              shapeLayer.lineWidth = 1.0
      
      //        Add the gradient for the view background if needed
              gradient.colors = [firstGradientColor.cgColor, secondGradiendColor.cgColor]
              gradient.startPoint = startPoint
              gradient.endPoint = endPoint
              gradient.frame = self.bounds
              gradient.mask = shapeLayer
      
              self.layer.addSublayer(gradient)
          }
      }
      
      1. 转到您的Storyboard,选择UIView 并在Identity Inspector 中写入类名TriangleView

      2. 享受你的三角! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-24
        • 1970-01-01
        • 1970-01-01
        • 2016-04-06
        • 1970-01-01
        • 1970-01-01
        • 2013-08-20
        相关资源
        最近更新 更多