A compendium of shapes using paths

Image for post
Image for post

In part 1 and 2 of this series I covered isosceles polygons, equilateral triangles, right + left angles triangles, parallelograms and trapezoids; putting together routines to build them using methods based on circles. You can find the stories here. Shapes that you can mutate and animate to your hearts content in code.

At the end of part 1 I mentioned that we would return to the triangles and indeed in part 2 developed a routine that I want to take further in this article. Further to try and do some more triangles. The plan to do the sort I missed in in part 1. …


A compendium of shapes using paths

Image for post
Image for post

In part 1 of this series I covered polygons principally, putting together routines to build them using paths a couple of methods based on circles. You can find the story here. Shapes that you can mutate and animate to your hearts content in code.

We covered a lot shapes in fact in part one, but well we still got a few to think about. What’s missing. We don’t have a parallelogram or a trapezoid. Two shapes that are vary similar.

Here is an Apple text book answer for a parallelogram.

struct Parallelogram: Shape {
@State var percent: Double
func path(in rect: CGRect) -> Path {
let edge = rect.width * CGFloat(percent/100)
var path = Path()
path.move(to: CGPoint(x: rect.minX + edge, y: rect.minY))
path.addLine(to: CGPoint(x: rect.maxX + edge, y: rect.minY))
path.addLine(to: CGPoint(x: rect.maxX - edge, y: rect.maxY ))
path.addLine(to: CGPoint(x: rect.minX - edge, y: rect.maxY))
path.closeSubpath() …


Where do generics fit in the big picture

Image for post
Image for post
Photo by Muhannad Ajjan on Unsplash

In the sixties, there were four major computer languages that ruled above all others. ALGOL, COBOL, FORTRAN, and LISP. With FORTRAN and ALGOL leading the way with typecasting. Although I think it was the son of ALGOL, PASCAL that took the most definitive steps as the decades past and ALGOL stepped aside. PASCAL was the language used to develop MacOS back then, and one that I suspect heavily influenced OBJECTIVE C design as well SWIFT.

Unsurprisingly perhaps, types haven’t changed that much in the last seventy years in fact. Which makes perfect sense since these concepts go back thousands of years. …


Looking at different ways to improve drawing

Image for post
Image for post
Redrawing a circle so that it will definatively be matched as a circular gesture

I published an article last week that talked about creating a custom gesture, a circular one. An article that is essentially an update to this excellent tutorial on the https://www.raywenderlich.com site. Although there was too much to say to truly do it justice. The key phrase in that paper was the “Taubin fit method” mentioned towards the end. One of half a dozen algorithms for trying to fit points to a circle.

Now assuming you’ve read it and/or better had a go — my article not Ray’s :) you would have realized that matching hand-drawn circles to geometric ones is a real challenge. A problem that mathematicians have been looking at for centuries. A problem for which you find quite a few more pre-cast solutions/alternative algorithms on the net. …


A couple of code challenges

If you’ve been learning to code in 2020, then maybe this short paper is for you. Within it I included a couple of swift challenges focused on closures. A key feature of many Apple frameworks. A super useful code component that you can use in your own apps; although the syntax can be a little cryptic at times. Find here two examples using closures, can you figure out what gets printed to the console.

What is the order that the numbers print out on the console?

Read through this piece of code carefully before you answer, here is a clue… it doesn’t rhyme with the phrase “you and me”. …


Learn to implement custom gestures that don’t come in the box

Example of custom gestures in SwiftUI
Example of custom gestures in SwiftUI
GIF by the author.

Being old school, when the iPad first arrived, I couldn’t imagine why anybody would want a device without a keyboard. I couldn’t have been more wrong because the touch-based interface that you get with an iPad is its defining difference. It is the thing that makes this product so user-friendly. In retrospect, I had forgotten that using a keyboard was second nature for me, but that wasn’t necessarily the case for your average person.

In this article, I want to explore the world of custom gestures in SwiftUI 2.0 …


A new way to change views

Image for post
Image for post
Segue between three views using delegation together with the Combine framework

I wrote an article a while back about changing views in SwiftUI, this one.

Within it suggesting the preferred method was using Published ObservableObjects. The best option of the choices presented, but playing around with protocols, delegation and singletons I think I found a better way. It is better way because — well deep down I have a quiet reservation with ObservedObjects since they are in effect global variables. Using the method presented here you can achieve the same sort flexible switching, without the need for global variables at all. Read on.

The brief — I am going to create three SwiftUI views all of which will be singletons along with with a class which will also be singleton that I’ll use to segue between them. I am going to use a protocol to make sure we’re all on the same page. …


An exploration of different methods to show and hide views in SwiftUI

Image for post
Image for post
Photo by Paul Skorupskas on Unsplash

SwiftUI is a framework all about views, or windows as they used to be called in ye old days. Views that you want to show and views that you’ll want to hide. Coding in SwiftUI is very different to UIKit because it uses declarative syntax that focuses on the logic and not the control flow. As such, understanding how to show and hide views can be less than obvious at times to a seasoned object-oriented coder, which the majority of us are. Let’s look at six different ways to do so.

Before I jump in, here is a quick reminder of an operator that is going to pop up several times in the discussion, that dastardly one called the ternary operator. An operator for which I can never remember which side is true and which is false. …


Mixing Protocol Programming with Declarative Coding

Image for post
Image for post
A simple app that synchronises the colour of the text/square using a delegate invoked with a tap gesture

There are dozens of programming paradigms with many languages supporting multiple ones. Swift is no exception and supports objected oriented coding with classes and inheritance, declarative coding with SwiftUI with states and protocol oriented programming with protocols and generic types. Indeed Swift’s support for the paradigm is one of the strongest for a leading language in the industry. Support that Dave Abrahams one of the principle architects behind Swift design and implementation describes as one of its primary goals.

All of which — is an odd state of affairs given you don’t find many tutorials or even papers on the subject — no sadly it seems almost everyone is still looking at Object-Oriented Coding even with Swift. So let’s break mould before the end of the year and take a short journey to explored how you can use protocols & delegation in your Swift code. …


A quick exploration of the options available in Swift to loop over an array

looping roller coaster
looping roller coaster
Photo by blueberry Maki on Unsplash

One of the most important elements in programming which most of us don’t give too much thought to is looping. I say that because, without looping, life in the world of coding would be almost impossible. And indeed, like most programming paradigms, there are many ways to do it. Let’s explore the world of repetition in Swift 5.0.

The code shown isn’t listed in any particular order really; it’s just a compendium of short code snippets you can use to loop the loop. That said, it gets gradually more complex as I move forward.

Ten Ways to Loop

The humble loop

This does what you might expect it to do: print out the directions one at a time on different lines. …

About

Mark Lucking

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store