Swift2Go

a place where Swift Developers share knowledge.

Follow publication

See-through UIViews

Bruno Muniz
Swift2Go
Published in
1 min readNov 17, 2020
Photo by Majid Gheidarlou on Unsplash

In the last couple of days I’ve been working on a QR code scanner feature (as you can probably tell by the topic of my last article) and I’ve stumbled upon a situation where I needed a semi-transparent overlay view which is completely transparent in the middle. In other words: a squared hole into an opaque view (or in this case semi-opaque).

CAShapeLayer + UIBezierPath will do the trick.

The trick is pretty straightforward — basically we need to get the path from our overlayView using UIBezierPath(rect: overlayView.bounds), then set fillRule from our CAShapeLayer to .evenOdd and then append the path of the view we want to crop. We could either do point/point…line/line using path.append or we can simply add a subvview to the overlayView with the size we want to crop and then append its bounds to the path.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Swift2Go
Swift2Go

Published in Swift2Go

a place where Swift Developers share knowledge.

Bruno Muniz
Bruno Muniz

Write a response