Custom views help give your app a more unique look and feel, and are surprisingly easy to create thanks to the power of UIKit and Core Graphics.
2 days ago May 10, 2020 SwiftUI is a declarative framework that you can use to build User Interfaces (UIs) for iOS, macOS, Mac Catalyst, tvOS, watchOS and iPadOS. Controls & User Input: Learn about controls such as TextField, Button, Toggle, Slider, Stepper, pickers and more. SwiftUI is the new UI All Apple platforms: iOS, iPadOS, watchOS and tvOS! With the first label being in place, the second quite common Cocoa control used in macOS apps is the text field. It’s a NSTextField object, and its purpose is to enable apps receiving user input when necessary. A text field is a single-line text input control, so if you want multiple lines of text you’d better pick a text view instead.
In this article we’re going to demonstrate custom controls by building a gauge control from scratch. There are lots of ways you can build gauge controls, so we’re going to try to add a number of customization points so that end users have control over the way our gauge looks.
SPONSOREDAre you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.
Creating the basic shape
Our first step is to create the basic gauge background. So, create a new iOS project in Xcode, using the Single View App template.
Create a new Cocoa Touch Class in there called GaugeView
, making it subclass from UIView
– that’s where we’ll do all our work. Finally, add this code to the viewDidLoad()
method of the ViewController
class:
That creates an instance of our gauge and places it on the screen so we can see what we’re working with every step of the way.
Check the version in one of the Office apps or go to Help Check for updates.The current Office for Mac; Office 365 or Office 2019 are OK with Catalina. Microsoft office for mac catalina crack 2017. There’s no workaround for this.Office 2016 for Mac needs to be v15.35 or later, which it probably is, if you’ve updated anytime in the last few months. Microsoft Office compatibilityAs we warned back in June,.If you have Office 2011 for Mac or before, do NOT update to Catalina because Office will stop working.
You won’t see anything yet, but we’ll fix that straight away: we’re going to give our gauge two bezels plus the main background. The second bezel is useful for creating a depth effect, or just to space the color segments away from the edges.
First, we need to define some properties:
- The color and width of the outer bezel.
- The color and width of the inner bezel.
- The main inside color of the gauge.
We need to make the width values instances of CGFloat
because we’ll be using them for Core Graphics calls, but I’m also going to provide some sensible default values for the colors: white on the inside and the inner bezel, and a medium blue for the outer bezel.
Add these properties to GaugeView
now:
We’ll use those immediately by adding a drawBackground()
method that draws the outer bezel, then the inner bezel, then the main body of the gauge as a series of concentric circles. Positioning and measuring the two inner circles can be done by calling insetBy()
on our drawing rectangle: once with the outer bezel width, then again with the inner bezel width.
Add this method to GaugeView
:
We can now call that when it’s time for our gauge to draw itself. This is done by implementing the draw()
method: it will start by finding the current drawing context, then call all the methods required to draw the gauge. We only have one of those right now, so let’s start there:
Go ahead and run the code now and you should see a blue ring. I know it might look pretty simple, but we’ve given users three different customization options and two size options already – they have a lot of control.
Adding gauge segments
Now for the first interesting step: we need to draw some colored segments along our gauge. This will have customizable colors and width, allowing folks to show a range of colors, mostly green then yellow and red at the end, or perhaps no colors at all if that’s the design they want.
Those customization points will be provided by two new properties: a CGFloat
called segmentWidth
for controlling how thick the gauge colors and drawn, and a UIColor
array called segmentColors
for controlling how the colors of each segment. I’m going to make mine have red segments at either end with green in the middle – imagine “low pressure”, “just right”, and “high pressure” – and I’m going to set the middle area to have multiple green segments to make it look more realistic.
Add these two properties to GaugeView
now:
Custom Controls For Ios Macos Tvos And Watchos Cocoa Controls 3
We’re also going to add two more customization points that control how the whole gauge is drawn:
- Where should the gauge start? Speedometers usually start at about 8pm on a clock face, but stop watches start at 12.
- How far should the gauge reach? Again, speedometers usually reach over to about 4pm.
We’ll store those as two extra properties, so please add these now:
OK, time for some actual code. We’re going to add a drawSegments()
method that will draw each segment. This takes quite a few steps, but the most important part is calling addArc()
once for each color segment of our gauge.
Here’s what it needs to do:
- Save the current configuration of our drawing context. We’re about to make changes, and we don’t want to pollute what comes next.
- Move our drawing context into the center of our draw rectangle, then rotate it so we’re pointing towards the start of the first segment. Moving like this means we’re draw relative to the center of our rectangle, which makes rotations more natural.
- Tell Core Graphics that we want to draw arcs using the size specified in our
segmentWidth
property. - Calculate the size of each segment by dividing the total angle of our gauge by the number of segments.
- Calculate the radius of our segment arc. This should be equal to the width of the draw rectangle minus our segment width, then halved. We halve the rectangle width because we want radius not diameter, and we halve the segment width because Core Graphics draws half the line over the radius and half under – we want it all under. Finally, we take away the outer and inner bezel widths.
- Loop over each segment color, drawing one piece of the arc at a time.
- Reset the graphics state to its earlier configuration.
It’s not a massive amount of code, and I’m going to try to break it down nice and small. Before we start, though, I want to add one little helper method that converts an angle from degrees to radians:
You’re welcome to leave your gauge working in radians if you prefer, but I think most folks find it easier to think in degrees so I’ll be converting here.
Here’s the new method, with comments matching the numbers above:
Tip: We need to subtract .pi / 2
from the rotation because Core Graphics measures its angles where zero degrees is directly to the right. Most users are likely to consider 0 degrees to be directly upwards, so we subtract 90 degrees (.pi / 2
in radians) to correct for that.
Now we just need to add a call to that in draw()
, after the drawBackground()
call:
If you run the code now you’ll see we now have colored segments inside the blue ring. You’ll also see why the inner bezel is helpful – it adds just a little spacing between the blue ring and the colored segments inside.
Adding tick lines
Now that we have segment bars across our gauge, the next step is to break them up into smaller chunks using tick lines. Again, we’re going to provide lots of customization points so that users can really go to town on configuration their gauge how they want:
- We’ll add major ticks and minor ticks, with major ticks being placed on the boundary between segments and minor ticks inside segments.
- Major ticks have a color, width, and length.
- Minor ticks also have a color, width, and length.
- Minor ticks also have a count – how many minor ticks we want inside each segment.
All those require new properties, so add these to GaugeView
now:
Once again, I’ve provided sensible defaults that mean users can customize only the bits they care about.
In fact, for the very best experience, you’ll definitely want those great features. With these features, you’ll be able to get the best out of the.When it comes to macOS Mojave, there’s a large number of fresh features and designs with advanced security. These features are the important part of every operating system you want to use. In this case, you’ll need to install the drivers for the specific feature but here’s the good news. Pro tools for mac os mojave download. In this case, you’ll need to install the drivers to work with.
Custom Controls For Ios Macos Tvos And Watchos Cocoa Controls Key
We need to write a third method called drawTicks()
, and this is a little bit more complicated again. First let’s fill in the easy bits:
- Save the state of the graphics context so we can restore back to a clean slate.
- Move into the center of our drawing and rotate to the start of the segments
- Calculate the segment angle and radius again.
- Save the state of the graphics context again so we can restore back to the translated/rotated position.
- Leave some space to draw the major ticks.
- Restore the graphics state, then save it again.
- Leave some space to draw the minor ticks.
- Restore the graphics state twice.
The main thing in there is the saving and restoring of graphics state. This might seem like a bit of a hassle, but it’s a sensible way of working: each of our drawing methods start and end with a clean slate so they don’t depend on each other, and the rotation transforms caused by drawing our major tick lines don’t affect drawing our minor tick lines.
Let’s start by writing code for the above. Add this method now:
There’s nothing surprising in there, so let’s move swiftly on to the real work: drawing the major and minor tick marks.
Drawing the major tick marks starts by adjusting the line width of our drawing context to match whatever was set in majorTickWidth
, then activating the majorTickColor
property. Replace the // draw major ticks
comment with this:
Next we need to loop over all the segment colors, drawing one major tick for each one. This will actually count from zero up to and including the number of segments, so that each segment starts and ends with a tick.
Now, we need a small amount of mathematics here in order to draw the correct tick line. We already calculated segmentRadius
, which is where we draw the colored bars, but remember that Core Graphics draws the segments half way over the radius and half under the radius.
We want our tick marks to end at the far edge of the segments, so we need add half the segment width. As for where the ticks start, we’ll take their end position and subtract the tick length, which will bring them closer to the center of the gauge.
So, add this code below the previous two lines:
Now all that’s left is to loop through all the segments, moving to the start point, adding a line to the end point, stroking it, then rotating so we’re ready to draw the next tick. Add this below the previous lines:
Drawing the minor ticks is similar, although we need a little extra mathematics. As before, we need to activate our new line width and color, then calculate the start and end points of the lines, so replace // draw minor ticks
with this:
However, this time we also need to figure out how far apart to draw the ticks. Minor ticks are drawn inside each segment, so if users ask for three ticks then we’ll draw three lines equally spaced inside each segment. To make that happen, we’ll divide the segment angle – the total angle for each segment – by minorTickCount
plus one.
The “plus one” part is important, because we draw the ticks inside the segments rather than at the ages. For example, if we had a segment angle of 100 and wanted three ticks, dividing 100 by three would place ticks at 33, 66, and 99 – there would be a tick right next to the major tick line at 100. But if we add one and skip the last tick, we get 25, 50, and 75, which divides the segment perfectly.
So, add this line of code next:
Now for the important part: we loop over all the segments, rotating a little each time, then draw the tick, then rotating again. The second rotation is required so that we don’t draw minor tick marks over the major tick marks by accident.
Add this code next:
That completes drawing the tick marks, so now you can go ahead and add a call to drawTicks()
to the end of the draw()
method, like thisL
If you run the code now you’ll see we have most of the gauge drawn now – all that remains is adding some sort of needle and read out on top. Canon mg8220 drivers for mac os mojave 10 14 iso.
Showing a value
To make the gauge actually useful takes three more steps: creating a needle that points to a specific value, rendering something behind the needle so that it looks like it’s fixed to our gauge, then adding a label that shows the current value.
Custom Controls For Ios Macos Tvos And Watchos Cocoa Controls Free
We’ll tackle those in the order 2, 1, 3, because that order makes sense in terms of the finished product as you’ll see. So, first we’re going to render something in the center of our gauge that the needle will be visually connected to.
To give the user maximum control, we’re going to create four new properties: the width and color of an outer disc, and the width and color of an inner disc. This allows us to make the inner disc the same color as the needle, giving it a pleasing round base, then make the outer disc look like the part attaching it to the gauge.
So, add these four properties to GaugeView
:
We’re going to use those four in a new method called drawCenterDisc()
. This will save and restore the graphics state as appropriate (always a good idea), move to the center of the rendering area, then draw two ellipses. Add this method now:
Remember also to add a call to that method in the draw()
method:
The next step is to add the gauge needle itself. This needs to be something we can move and animate freely, so it’s a smart idea to make a separate UIView
for this rather than trying to draw it by hand.
So, start by adding these three properties to GaugeView
:
Now we need to make a method that configures the needle and positions it correctly. This is easier said than done: we want the needle to rotate from its bottom edge rather than UIKit’s default center, so we need to adjust the anchor point of its CALayer
before we position it. We also need to make sure this set up method gets called from both of the initializers of UIView
, because it could be created from code or from a storyboard.
So, first add this setUp()
method to GaugeView
:
We can then add both initializers for the view, each time calling setUp()
:
Finally, we need to add a UILabel
to show the actual value of the gauge. Again, we need to expose some customization points for color and font, so add these properties to GaugeView
:
Just like the needle, we need to configure valueLabel
when setUp()
is called. So, add this code to the setUp()
method so the label is positioned at the center bottom of the gauge:
Bringing it to life
Now that we’ve drawn all the components for our gauge, the last part is relatively straightforward: we need to add a value
property that updates the needle position and text label to reflect changes.
I’m going to make my gauge store a value between 0 and 100, which can then be mapped to the range 0 through 1 to decide where the needle ought to point. This will be done using a lerp – a linear interpolation – which will calculate an angle between the needle’s start and end angles based on the value that was passed in. So, if the start angle is 0 and the end is 200, and the value is 50 (i.e., half way), the lerp will return 100.
So, add this property now:
That completes our gauge! All that remains now is to try out changing its value. This is best done using a delay so that the label updates correctly, so try adding this to viewDidLoad()
in the ViewController
class:
Go ahead and run the code one last time, and you should see the label and needle change with the value – good job!
Custom Controls For Ios Macos Tvos And Watchos Cocoa Controls 8
Where next?
I tried to make this control demonstrate a variety of techniques, but there’s still a lot more it can do. If you’re looking to take it further, try some of these:
Custom Controls For Ios Macos Tvos And Watchos Cocoa Controls Pc
- Make the label text animate up or down.
- Stop the needle from moving below its minimum position – a problem if you animate moving from value 0 to value 100.
- Go through all the methods and properties and mark as many as
private
as you can. - Make the class
@IBDesignable
and@IBInspectable
as much as is possible. - Add an
accessibilityLabel
that reads the value of the gauge to folks using Voiceover. - Make sure that changing properties triggers a redraw / update of the gauge.
Custom Controls For Ios Macos Tvos And Watchos Cocoa Controls 2
And those are just to start with! Building a custom control is fun, but you soon realize the possibilities are almost endless – enjoy!
Custom Controls For Ios Macos Tvos And Watchos Cocoa Controls 10
SPONSOREDAre you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.