UIBezierPath Fill上的Alpha组件

时间:2016-08-01 11:45:10

标签: ios swift uibezierpath uicolor alpha-transparency

我的视图定义如下:

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 {1}}在这两种情况下,颜色变得越暗。我不确定我做错了什么。

2 个答案:

答案 0 :(得分:2)

将观看次数createObject()设置为backgroundColor

答案 1 :(得分:0)

我认为您为 ViewController 上的主视图指定了 TriangleView 。而不是从对象库拖动另一个视图并将其指定为 TriangleView