Search code examples
iosswiftuiimageviewswift3calayer

How to add color over CALayer() with alpha value?


I have working slideshow codes with CALayer() and I want to add Brown color mask over images with alpha value. My CALayer() codes under below.

   let imageLayer = CALayer()
    imageLayer.contents = image.cgImage
    imageLayer.anchorPoint = CGPoint.zero
    imageLayer.bounds = CGRect(origin: CGPoint.zero, size: optimus)
    imageLayer.position = origin
    imageView.layer.addSublayer(imageLayer)

Full code you can see here: https://github.com/Gatada/JBKenBurnsView/blob/master/KenBurns/JBKenBurnsView.swift. Look at 281-286 lines between.


Solution

  • Try like this.

    imageLayer.backgroundColor = UIColor.brown.withAlphaComponent(0.8).cgColor //Set alpha you want
    

    Edit: From your edit code you need to set your imageLayer2 frame like this.

    imageLayer2.frame = imageView.bounds