Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
Recorte una UIView con una ruta circular Swift iOS

Estoy tratando de recortar la vista previa de mi cámara (UIView) dentro de una ruta circular, pero parece que la máscara no recorta mi vista.

ingrese la descripción de la imagen aquí

 let preview = cameraController!.previewLayer preview.translatesAutoresizingMaskIntoConstraints = false _view.translatesAutoresizingMaskIntoConstraints = false let path = UIBezierPath(arcCenter: CGPoint(x: _view.frame.size.width/2, y: _view.frame.size.height/2), radius: _view.frame.size.width/2.5, startAngle: CGFloat(0).toRadians(), endAngle: CGFloat(360).toRadians(), clockwise: false) path.reversing() let shapeLayer = CAShapeLayer() shapeLayer.path = path.cgPath shapeLayer.fillColor = UIColor.clear.cgColor shapeLayer.strokeColor = UIColor.blue.cgColor shapeLayer.lineWidth = 5 preview.layer.mask = shapeLayer preview.layer.addSublayer(shapeLayer) preview.clipsToBounds = true _view.addSubview(preview)

¿Qué me estoy perdiendo aquí?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

La mayor parte de la configuración estuvo bien, solo cambié algunas cosas para que funcione:

 func maskPreview() { // No change from your code let path = UIBezierPath(arcCenter: CGPoint(x: view.frame.size.width/2, y: view.frame.size.height/2), radius: view.frame.size.width/2.5, startAngle: CGFloat(0).toRadians(), endAngle: CGFloat(360).toRadians(), clockwise: false) // Removed this, not sure it was needed // Add it back if needed //path.reversing() // No change let shapeLayer = CAShapeLayer() shapeLayer.path = path.cgPath // This should not be transparent since we need // the camera preview to be seen through here shapeLayer.fillColor = UIColor.white.cgColor // No change shapeLayer.strokeColor = UIColor.blue.cgColor shapeLayer.lineWidth = 5 // No change previewView.layer.mask = shapeLayer // You do not need to add the shape layer as a sublayer // after defining it as your mask // previewView.layer.addSublayer(shapeLayer) // No change previewView.clipsToBounds = true }

Resultado final

Máscara Cámara Vista previa Círculo Ruta UIBezierPath CAShapeLayer Clip UIView Vista previa Cámara AVFoundation

Actualización para discutir sobre el color de las capas de máscara.

La documentación de la mask define este comportamiento en el que el canal alfa determina qué contenido, si lo hay, se le permitirá mostrar.

máscara

Una vista opcional cuyo canal alfa se usa para enmascarar el contenido de una vista.

Discusión

El canal alfa de la vista determina qué parte del contenido y el fondo de la vista se muestra. Los píxeles total o parcialmente opacos permiten que se muestre el contenido subyacente, pero los píxeles totalmente transparentes bloquean ese contenido.

El color de relleno no es el color del círculo como bien señaló. Podrías cambiar esto y no importaría siempre y cuando no sea de color claro. Determina si la máscara representa la vista subyacente o no.

Para agregar algo de color a esto, creo que no puede hacerlo directamente a través de la capa de máscara y necesitaría una capa intermedia.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!