我有一个这样定义的观点:

class TriangleView: UIView {


override func drawRect(rect: CGRect) {

    // Get Height and Width
    let layerHeight = self.layer.frame.height
    let layerWidth = self.layer.frame.width

    // Create Path
    let bezierPath = UIBezierPath()

    bezierPath.moveToPoint(CGPointMake(0, 0))
    bezierPath.addLineToPoint(CGPointMake(0, layerHeight))
    bezierPath.addLineToPoint(CGPointMake(layerWidth, layerHeight + 4))
    bezierPath.addLineToPoint(CGPointMake(0, 0))
    bezierPath.closePath()

    // Apply Color
    UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0).setFill()
    bezierPath.fill()

    // Mask to Path
    let shapeLayer = CAShapeLayer()
    shapeLayer.borderColor = UIColor.clearColor().CGColor
    shapeLayer.path = bezierPath.CGPath
    self.layer.mask = shapeLayer

}

}

我正在尝试使其半透明,但是使用UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 0.5).setFill()以及使用bezierPath.fillWithBlendMode( .Normal, alpha: 0.5)会产生比正常颜色更暗且没有透明度的相同结果,在两种情况下我减少alpha的次数越多,颜色就会变得越深。我不确定自己在做什么错。

最佳答案

将视图backgroundColor设置为UIColor.clearColor()

10-02 11:52