Understanding Swift’s ConvertPoint

Which coordinate system are you in?

Steven Curtis
5 min readMar 15

--

Photo by Ibrahim Boran on Unsplash

Before we start

Difficulty: Beginner | Easy | Normal | Challenging

This article has been developed using Xcode 14.2, and Swift 5.7.2

Keywords and Terminology:

Bounds: Used in a Custom UIView to represent the drawing space’s origin and size

Frame: A rectangle in a superview’s coordinate space that contains the view’s bounds.size in its entirety

Prerequisites:

  • You need to be able to create a basic Swift App. The example application I’ve built is created programmatically

The example App

This has been created programmatically, and essentially is three UIView instances placed on a plain background. Please be aware that I’ve set the background as red to attempt to make it as easy as possible to see the two views on top of it.

I’ve created the three views as lazy vas. These have separate colours as follows -

view1: blue

view2: green

view3: orange

and essentially each view is the same, but here are the details for the first view:

lazy var view1: UIView = {
// setup `UIView` instance
let view = UIView()
// set the colour
view.backgroundColor = .blue
// let me take care of the layout
view.translatesAutoresizingMaskIntoConstraints = false
// create a `UITapGestureRecognizer` - the target is ViewController, and the function to be run is the tap function defined in this class
let touch = UITapGestureRecognizer(target: self, action: #selector(tap(sender:)))
// add the gesture to this view
view.addGestureRecognizer(touch)
// return this view
return view
}()

where the gesture recognizer is set up as follows, but more on the content of this function later!

@objc func tap(sender: UITapGestureRecognizer) { }

--

--