UIView带有多个遮罩

huangapple go评论63阅读模式
英文:

UIView with multiple masks

问题

这是您的代码部分,不需要翻译:

let maskLayer = CAGradientLayer()
maskLayer.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
let innerColor = UIColor(white: 1.0, alpha: 1.0).cgColor
let outerColor = UIColor(white: 1.0, alpha: 0.0).cgColor
maskLayer.colors = [outerColor, innerColor, innerColor, outerColor]
maskLayer.locations = [0.0, 0.20, 0.80, 1.00]
maskLayer.startPoint = CGPointMake(0.5, 0);
maskLayer.endPoint = CGPointMake(0.5, 1);
self.myView.layer.mask = maskLayer

这是问题部分:

"Is there a way for me to add an additional circle mask (non-gradient) to the center of the view?"

这段文字的翻译是:

"有没有办法让我在视图中央添加一个额外的圆形遮罩(非渐变)?"

英文:

I currently have a UIView with a gradient mask:

let maskLayer = CAGradientLayer()
maskLayer.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
let innerColor = UIColor(white: 1.0, alpha: 1.0).cgColor
let outerColor = UIColor(white: 1.0, alpha: 0.0).cgColor
maskLayer.colors = [outerColor, innerColor, innerColor, outerColor]
maskLayer.locations = [0.0, 0.20, 0.80, 1.00]
maskLayer.startPoint = CGPointMake(0.5, 0);
maskLayer.endPoint = CGPointMake(0.5, 1);
self.myView.layer.mask = maskLayer

This adds a gradient mask to the top and bottom of the view successfully.

Question

Is there a way for me to add an additional circle mask (non-gradient) to the center of the view?

答案1

得分: 1

你可以创建一个父/容器层,并将所有的遮罩层添加为其子层。

let containerMaskLayer = CALayer()

let gradientLayer = CAGradientLayer()
gradientLayer.frame = CGRect(x: 0, y: 0, width: 300, height: 500)
let innerColor = UIColor(white: 1.0, alpha: 1.0).cgColor
let outerColor = UIColor(white: 1.0, alpha: 0.0).cgColor
gradientLayer.colors = [outerColor, innerColor, innerColor, outerColor]
gradientLayer.locations = [0.0, 0.20, 0.80, 1.00]
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0);
gradientLayer.endPoint = CGPoint(x: 0.5, y: 1);
containerMaskLayer.addSublayer(gradientLayer)

let circleLayer = CAShapeLayer()
let radius: CGFloat = 100.0
circleLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 2.0 * radius, height: 2.0 * radius), cornerRadius: radius).cgPath
circleLayer.position = CGPoint(x: 100 - radius, y: 100 - radius)
circleLayer.fillColor = UIColor.white.cgColor
containerMaskLayer.addSublayer(circleLayer)

view.layer.mask = containerMaskLayer
英文:

You can create a parent/container layer and add all the mask layer's as it's sublayer.

let containerMaskLayer = CALayer()

let gradientLayer = CAGradientLayer()
gradientLayer.frame = CGRect(x: 0, y: 0, width: 300, height: 500)
let innerColor = UIColor(white: 1.0, alpha: 1.0).cgColor
let outerColor = UIColor(white: 1.0, alpha: 0.0).cgColor
gradientLayer.colors = [outerColor, innerColor, innerColor, outerColor]
gradientLayer.locations = [0.0, 0.20, 0.80, 1.00]
gradientLayer.startPoint = CGPointMake(0.5, 0);
gradientLayer.endPoint = CGPointMake(0.5, 1);
containerMaskLayer.addSublayer(gradientLayer)

let circleLayer = CAShapeLayer()
let radius: CGFloat = 100.0
circleLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 2.0 * radius, height: 2.0 * radius), cornerRadius: radius).cgPath
circleLayer.position = CGPoint(x: 100 - radius, y: 100 - radius)
circleLayer.fillColor = UIColor.white.cgColor
containerMaskLayer.addSublayer(circleLayer)

view.layer.mask = containerMaskLayer

huangapple
  • 本文由 发表于 2023年5月13日 22:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定