Custom UIView in Swift, that’s a simple joystick-style interface.

Custom UIView in Swift, that’s a simple joystick-style interface. The custom view consists of two cases of UIImageView
, one for the base and one for the pen. When the operator moves the handle, he detects
according to its position in relation to the base of the joystick. The type of information sent depends on the type
monitor installed:

  • JoyStickViewMonitorKind.polar — reports on JoyStickViewPolarReport examples with
    • Angle – grip direction – point set in degrees, north/up – 0°, east/right – 90°.
    • Offset – the distance to the centre where the handle was moved, from 0.0 to 1.0 of 1.0.
  • JoyStickViewMonitorKind.xy — reports cases of JoyStickViewXYReport using
    • x — horizontal shift relative to the centre of the base, where east/right is positive
    • y — vertical shift from the center of the base, where north/top is positive.

Let me take a look: JoyStickViewPolarMonitor = {
print((format: %.2f°, $0.angle))) (string(format: %.3f, $0.offset)))).

joystick.monitor = .polar(monitor: monitor2)

There is also support (3.0.1) for using the C-Lens unit as a monitor, with a slight decrease in safety type
. Both setPolarMonitor and setXYMonitor accept a termination that accepts two CGFloat arguments, and
does not return a value. These methods can use Objective-C blocks as well as Swift windows.

The view supports the (mobile) option, where the view moves when the user moves the
handle further than 1.0. This can be useful if the starting position of the joystick in an application is not ideal for the
thumb. A double pressure on the joystick brings it back to the starting position.

In the animation above there are two joysticks, a green one and a purple one. The green is fixed and will not move
, even if the touch causes a shift of more than 1.0. The magenta joystick, on the other hand, is a mobile
joystick whose base follows the movement of the touch. For mobile joysticks, the MovableBounds Base motion feature is optionally limited to
CGRect, as in the demo animation on
, where the magenta colored joystick cannot move off the pink band.

Additional characteristics

Here are some additional customizable features of JoyStickView :

  • HandleConstraint is an optional CGRect that restricts the movement of the handle. Look at the example of the playground.
  • baseImage – UIImage for the base of the joystick.
  • handleImage – UIImage for the joystick handle.
  • the formation of the Alpha… …the opacity of the base of the joystick.
  • the alpha handle is the opacity of the joystick handle.
  • handleTintColor. Extra shadow color applied to the joystick image.
  • handleSizeRatio is the zoom applied to the image of the joystick button. Please note that for historical reasons the default setting is 0.85.
  • enableDoubleTapForFrameReset — if Move is true, the user can double-click on the view to move the base to the starting position
  • v3.0.2. OverscalingCorrectionHandleImageView
  • v3.0.1. Add support for Obj-C monitor blocks
  • v3.0.0 — Swift 5 (no code change, only Xcode configuration)
  • v2.1.2 — fast 4.2

The Xcode workspace consists of three elements:

Both the playground and the application are based on the JoyStickView UIView framework.

The Xcode of the playing field adjusts the display environment and adjusts two joysticks, one fixed (green)
and the other mobile (yellow). The two levers indicate their position in two marks, one for the corners and one for the offset.

The JoyStickView.fast file defines the appearance and operation of the joystick. It is located within the framework of JoyStickView. You will also find the CoreGraphics+Additions.fast file, which contains several extensions of some CoreGraphics structures that allow you to use simplified mathematical expressions in the JoyStickView code.

By default, the JoyStickView class uses two images located in the Images folder

  • JoyStickBase*.png – image to use as a joystick base
  • JoyStickHandle*.png is the image you can use with the joystick handle. Hint: It will be colored when you configure handleTintColor.

Both are available in three resolutions for the different iOS devices available today. They are made with the great application of Opacity. Opaque documents are included in this repository in the Resource Catalogue.

To use your own images, simply define the baseImage and/or handleImage attributes using the desired UIImage.

For more information, see the documentation on codes.

A simple CocoaPods specification file is available for you to add code and
sources by adding BRHJoyStickView to your pod file. For now it works more or less, except that
pointing to image sources via Interface Builder (IB) leads to invalid image results, because
cannot find the files where IB could find them. The only solution is to find these files manually and install them in download code type
. Something like this should help:

Extension beam {

Find the internal ligament in the CocoaPod package.

– Parameter name : Name of all internal resources. It must match the s.resource_bundle or
key with one of the s.resoruce_bundles keys in the podspec file that defines the cocoPod.
– get it: Source bundle or `self` if no source bundle is found
podResource(name: string) -> Bundle {
keeps bundleUrl = self.url(forResource: name, with the extension: bundle) otherwise { return itself }.
Return of the package (url: bundleUrl) ??self

You must do something similar in your installation code:

Override the viewDidLoad() function {super.viewDidLoad()let bundle = Bundle(for: JoyStickView.self).baseImage = UIImage(Name: FancyBase, in: Bundle, compatible with: null)joystick.handleImage = UIImage(Name: FancyHandle, in: Bundle, compatible with: null)}.

The podResource method tries to find an internal link with the name. If it is not found, it will be in default of its original commitment. The viewDidLoad
code uses the correct package object in UIImage constructors.


connect storyboard to viewcontroller,swift show view controller,viewcontroller.swift missing,add view controller as subview swift,multiple views in one view controller swift,swift create view controller programmatically,swift custom view programmatically,swift custom view xib,custom uiview in storyboard,custom uiview with xib objective-c,swift custom uiview with button,customizing uiview,swift create view programmatically,uinavigationcontroller view hierarchy,what is root view controller swift,swift nested navigation controllers,navigation controller not working swift,how to add navigation controller in swift 4,create view programmatically swift 5,add uiview,swift programmatically,adding uiview to viewcontroller,add view on view swift,create custom uiview programmatically swift 5,uihostingcontroller,uihostingcontroller<anyview,nshostingcontroller example,uikit in swiftui,uihostingcontroller environmentobject,uihostingcontroller background color,custom uiview objective-c,objective c instantiate xib,uiview load from nib objective c,objective c instantiate nib,objc init view from nib,objc load from nib,container view controller multiple views,container view swift,container view in storyboard,container view ios objective c,pass data to container view swift,container view ios swift 4,uiview init objective-c,swiftui view init,init(coder:) has not been implemented,swift subclass uiview,custom uiview swift,add view to viewcontroller,load uiview from xib programmatically swift,uiview — swift 5

You May Also Like

🥇 Calculate the Cost of a Product in Excel  Step by Step Guide ▷ 2020

If you want to sell a product or offer a service to…

🥇 MICROSOFT POWERPOINT  What is it? + Alternatives ▷ 2020

One of the advantages of using Office software packages is that you…

Pytorch Image Augmentation using Transforms.

In-depth learning models generally require a lot of data for learning. In…

Fix: Google Photos not backing up on iPhone

For many iPhone users, Google Photos remains the first choice, even though…