英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论