Quick Start Guide - Swift

Introduction

The following guide will get you up and running with the SGauge component as quickly as possible. In this guide, we will introduce you to the key features of the Gauge, including initial project setup and configuring the style. It may help to follow along with our related code sample: GaugesGettingStartedSwift.xcodeproj

Setting up the project

Start up Xcode and create a new project (File / New / Single View Application). Chose swift as the langugage to use for your new project.

Within your newly created project, add a reference to the ShinobiGauges.framework. Instructions for doing this can be found in the Framework Guide. If you are a trial user, you will also need to setup the trial key.

Linking to the bridging header file

In order for Xcode to use an Objective-C based framework in Swift, it needs to have a bridging header file. We have included a bridging header file in our framework. To link to it, you must open the build settings for the your new target, and search for the Objective-C Bridging Header setting. You must then provide the path to the ShinobiGauges-Bridging-Header.h file, which is inside the Headers directory of the ShinobiGauges.framework.

Setting the Swift project's bridging header file

In the screenshot above, you can see that the framework is three directories above the root of the project, hence it is set to

$(SRCROOT)/ShinobiGauges.framework/Headers/ShinobiGauges-Bridging-Header.h

This path will vary based on the location of the framework on your file system.

Creating a Gauge

First, open up ViewController.swift and in viewDidLoad, create the Gauge instance as follows:

override func viewDidLoad() {
    super.viewDidLoad()

    let gauge = SGaugeRadial(frame: CGRectMake(0, 0, 400, 400), fromMinimum: 0, toMaximum: 1000)
    gauge.center = view.center
}

At this point, build and run the project to check that everything has been set up correctly. You should see something similar to the gauge below:

Configuring the Gauge

The gauge has been built with styling in mind, allowing you to change virtually every aspect of the gauge, needle and the axis. For more detail on this, see the Styling section below.

To customise the default style, add the following lines to the end of viewDidLoad:

gauge.style = SGaugeDashboardStyle()

This will apply the “dashboard” style. Next, make the range take up more of the circle by setting the start and end angle of the axis arc:

gauge.arcAngleStart = CGFloat(-M_PI_4 * 3)
gauge.arcAngleEnd = CGFloat(M_PI_4 * 3)

Thicken the bevel around the edge:

gauge.style.bevelWidth = 30

Finally, give the gauge a value:

gauge.value = 375

This is only a small part of what the Gauge can do, but there are plenty of options on how to customize it exactly how you need it. If you got stuck at any point, take a look at our related code sample: GaugesGettingStartedSwift.xcodeproj.