Triangle UIView - Swift

前端 未结 5 1575
长情又很酷
长情又很酷 2020-12-05 06:43

So I\'m making a game in which I am dropping objects which have to be destroyed by a spike(triangle) at the bottom of the screen by a user.

I cannot work out how to

5条回答
  •  死守一世寂寞
    2020-12-05 07:21

    CAShapeLayer it can change the shape of layers.

        var mask = CAShapeLayer()
        mask.frame = self.layer.bounds
    
        let width = self.layer.frame.size.width
        let height = self.layer.frame.size.height
    
        var path = CGPathCreateMutable()
    
        CGPathMoveToPoint(path, nil, 30, 0)
        CGPathAddLineToPoint(path, nil, width, 0)
        CGPathAddLineToPoint(path, nil, width, height)
        CGPathAddLineToPoint(path, nil, 0, height)
        CGPathAddLineToPoint(path, nil, 30, 0)
    
        mask.path = path
    
        // CGPathRelease(path); - not needed
    
        self.layer.mask = mask
    
        var shape = CAShapeLayer()
        shape.frame = self.bounds
        shape.path = path
        shape.lineWidth = 3.0
        shape.strokeColor = UIColor.whiteColor().CGColor
        shape.fillColor = UIColor.clearColor().CGColor
    
        self.layer.insertSublayer(shape, atIndex: 0)
    

提交回复
热议问题