Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.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 使用CGRect框架在UIView中生成三角形_Ios_Uiview_Swift_Cgrect - Fatal编程技术网

Ios 使用CGRect框架在UIView中生成三角形

Ios 使用CGRect框架在UIView中生成三角形,ios,uiview,swift,cgrect,Ios,Uiview,Swift,Cgrect,嗨,我正在做一个游戏,需要在底部钉一个钉子,我已经决定通过UIView和碰撞来做。我用swift编码 我现在有一个正方形: //对象设置 let square=ui视图(帧:CGRect(x:100,y:100,宽度:100,高度:100)) square.backgroundColor=UIColor.purpleColor() 视图。添加子视图(方形) 我想要一个三角形,我有一个可以用来画三角形的图像,但是图像是正方形的,所以当它接触到图像边界时,碰撞肯定会发生,而不是实际的分流边界。请告

嗨,我正在做一个游戏,需要在底部钉一个钉子,我已经决定通过UIView和碰撞来做。我用swift编码

我现在有一个正方形:

//对象设置
let square=ui视图(帧:CGRect(x:100,y:100,宽度:100,高度:100))
square.backgroundColor=UIColor.purpleColor()
视图。添加子视图(方形)
我想要一个三角形,我有一个可以用来画三角形的图像,但是图像是正方形的,所以当它接触到图像边界时,碰撞肯定会发生,而不是实际的分流边界。请告诉我如何处理图像或者我是如何得到正方形的

谢谢


Alex

这里有教程:

代码如下:

func drawPlayPathTo(context: CGContextRef, boundedBy rect: CGRect) {
  CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)
  CGContextMoveToPoint(context, rect.width / 4, rect.height / 4)
  CGContextAddLineToPoint(context, rect.width * 3 / 4, rect.height / 2)
  CGContextAddLineToPoint(context, rect.width / 4, rect.height * 3 / 4)
  CGContextAddLineToPoint(context, rect.width / 4, rect.height / 4)
  CGContextFillPath(context)
}
步骤:
  • 创建一个名为
    TriangleView
  • 将其粘贴到
    TriangalView
    类中
  • 通过
    XIB
    或以编程方式添加
    TriangleView


  • 结果:

    您是否考虑过在游戏中使用SpriteKit?使用SpriteKit,真正的“形状匹配”物理和碰撞非常容易,但是当你创建游戏时,UIKit会很快达到极限。Swift 2编译器可能会告诉你,你的大部分
    var
    s应该是
    let
    s,因为你在创建它们之后不会对它们进行变异。这包括
    beizerPath
    ,因为您改变的是Objective-C对象,而不是指针。如何使用此方法传入自定义参数或颜色?@AndrewAnthonyGerst检查一下-只需对
    UIView
    进行
    快速扩展即可。您可以在1个类中创建扩展,也可以从任何其他类访问它@MikeMilla谢谢你的及时回复mike!不过我确实想出来了。我创建了一个新的构造函数,以便传入自定义参数
    let triangleView=triangleView(frame:CGRectMake(0.0,0.0,50.0,50.0),dir:“down”,color:UIColor.orangeColor())
    你知道有没有办法在三角形周围添加阴影?效果很好。
    import UIKit
    
    class TriangleView: UIView {
    
        override func draw(_ rect: CGRect) {
    
            // Get Height and Width
            let layerHeight = layer.frame.height
            let layerWidth = layer.frame.width
    
            // Create Path
            let bezierPath = UIBezierPath()
    
            // Draw Points
            bezierPath.move(to: CGPoint(x: 0, y: layerHeight))
            bezierPath.addLine(to: CGPoint(x: layerWidth, y: layerHeight))
            bezierPath.addLine(to: CGPoint(x: layerWidth / 2, y: 0))
            bezierPath.addLine(to: CGPoint(x: 0, y: layerHeight))
            bezierPath.close()
    
            // Apply Color
            UIColor.green.setFill()
            bezierPath.fill()
    
            // Mask to Path
            let shapeLayer = CAShapeLayer()
            shapeLayer.path = bezierPath.cgPath
            layer.mask = shapeLayer
        }
    }