Recipe: Subclassing UIControl

UIKit provides many prebuilt controls that you can use directly in your applications. There are buttons and switches and sliders and more. But why stop there? You don’t have to limit yourself to Apple-supplied items. Why not create your own?

Recipe 2-5 demonstrates how to subclass UIControl to build new controls from scratch. This example creates a simple color picker. It lets the user select a color by touching or dragging within the control. As the user traces left and right, the color changes hue. Up and down movements adjust the color’s saturation. The brightness and alpha levels for the color are fixed at 100%.

This is a really simple control to work with because there’s not much interaction involved beyond ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.