GithubHelp home page GithubHelp logo

joystick's Introduction

Joystick

A custom UIView in Swift that presents a simple joystick interface. The custom view consists of two UIImageView instances, one for the base and one for the handle. Moving the handle generates two values

  • angle — the direction the handle is point, given in degrees, where north/up is 0° and east/right is 90°
  • displacement — the distance from the center the handle moved, from 0.0 to 1.0 with 1.0.

The view supports an option (movable) where the view will move when the user moves the handle to a displacement beyond 1.0. This can be useful when the initial position of the joystick in an app is not ideal for the user's thumb. Double-tapping on the joystick moves it back to its original position.

In the animation above, there are two joysticks, one green and one red. The green is fixed and does not move even when the touch motion would cause a displacement larger than 1.0. The yello joystick however is movable, with the base following the touch motion. Base motion is optionally restricted to a CGRect, as is the case in the demonstration animation above where the yellow joystick cannot move into the red band.

Code

The Xcode playground code sets up the display environemnt and installs two joysticks, one that is fixed (green) and the other that is movable (red). Both joysticks report out their positions in two labels, one for angles and the other for displacement.

The JoyStickView.swift file that defines the joystick view and behavior resides inside the playground in the Sources directory inside of the playground package. There is also a file there (CoreGraphics+Additions.swift) that contains various extensions to some CoreGraphics structs that allow for some simplified mathematical expressions in the joystick code.

The JoyStickView.swift depends on two image assets found in the Resources folder:

  • JoyStickBase*.png — the image to use for the base of the joystick
  • JoyStickHandle*.png — the image to use for the handle of the joystick. Note: this will be tinted with the handleTintColor setting (defaults to the view's tintColor parameter)

Both exist in three resolutions for the various iOS devices out today. They were generated using the great Opacity app. The Opacity documents are included in this repository at the top-level (JoyStickBase.opacity and JoyStickHandle.opacity)

joystick's People

Contributors

bradhowes avatar

Watchers

James Cloos avatar Shun avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.