🕹Analog Joystick on swift with closures
Swift
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
AnalogStick Demo Swift 4 Aug 19, 2017
License Update License Apr 20, 2017
README.md Update README.md Apr 18, 2018
preview.gif preview gif Aug 21, 2017

README.md

AnalogJoystick.swift

Preview

Analog joustick

Features

  • Begin handler
  • Tracking handler
  • Stop handler
  • Set/change joystick diameter
  • Set/change stick && substrate colors
  • Set/change stick && substrate images

Manual

  1. Just drop the AnalogJoystick.swift file into your project.
  2. That's it!

Init examples:

init with 100px diameter.Colors & images you can change later

let joystick = AnalogJoystick(diameter: 100)
// or
let joystick = 🕹(diameter: 100)

Substrate has 100px diameter, stick has 50px diameter

let joystick = AnalogJoystick(diameters: (100, 50)) 

init with 100px diameter.Substrate has blue color, stick has yellow color

let joystick = AnalogJoystick(diameter: 100, colors: (UIColor.blue(), UIColor.yellow()))

init with 100px diameter.Substrate has "substrate" image, stick has "stick" image

let joystick = AnalogJoystick(diameter: 100, images: (UIImage(named: "substrate"), UIImage(named: "stick")))

init with 100px diameter.Substrate has blue color && "substrate" image, stick has yellow color && "stick" image

let joystick = AnalogJoystick(diameter: 100, colors: (UIColor.blue(), UIColor.yellow()), images: (UIImage(named: "substrate"), UIImage(named: "stick")))

init with substrate && stick diameters.Substrate has blue color && "substrate" image, stick has yellow color && "stick" image

let joystick = AnalogJoystick(diameters: (100, 50), colors: (UIColor.blue(), UIColor.yellow()), images: (UIImage(named: "substrate"), UIImage(named: "stick")))

init with substrate && stick diameters.Substrate has blue color, stick has yellow color

let joystick = AnalogJoystick(diameters: (100, 50), colors: (UIColor.blue(), UIColor.yellow()))

init with substrate && stick diameters.Substrate has "substrate" image, stick has "stick" image

let joystick = AnalogJoystick(diameters: (100, 50), images: (UIImage(named: "substrate"), UIImage(named: "stick")))

Designated initializator

init(substrate: AnalogJoystickSubstrate, stick: AnalogJoystickStick)

WHERE:

  • substrate - substrate of joystick (AnalogJoystickSubstrate:AnalogJoystickComponent)
  • stick - stick of joystick (AnalogJoystickStick:AnalogJoystickComponent)

Convenience initializators:

convenience init(diameters: (substrate: CGFloat, stick: CGFloat?), colors: (substrate: UIColor?, stick: UIColor?)? = nil, images: (substrate: UIImage?, stick: UIImage?)? = nil)
convenience init(diameter: CGFloat, colors: (substrate: UIColor?, stick: UIColor?)? = nil, images: (substrate: UIImage?, stick: UIImage?)? = nil)

Typealias

typealias AnalogJoystick = 🕹

Example

let joystick = 🕹(diameter: 110) // it's equal let joystick = AnalogJoystick(diameter: 110)

Handlers

  • var beginHandler: (() -> Void)? // before move
  • var trackingHandler: ((AnalogJoystickData) -> ())? // when move
  • var stopHandler: (() -> Void)? // after move

Computed Properties

  • var stickColor: UIColor (get/set)
  • var substrateColor: UIColor (get/set)
  • var stickImage: UIImage? (get/set)
  • var substrateImage: UIImage? (get/set)
  • var diameter: CGFloat (get/set)

Examples

Create joystick

let joystick = AnalogJoystick(diameter: 100) // you can set images/color later

or with images

let joystick = AnalogJoystick(diameter: 100, images: (UIImage(named: "substrate"), UIImage(named: "stick")))

Tracking With Closure

joystick.beginHandler = { [unowned self] in
  // Something...
}

joystick.trackingHandler = { [unowned self] data in
  // Something...
  // data contains angular && velocity (data.angular, data.velocity)
}

joystick.stopHandler = { [unowned self] in
  // Something...
}

Change diameter

  joystick.diameter = 100 // set new diameter

Change colors

  joystick.stick.color = UIColor.yellow() // set yellow color to stick node
  joystick.substrate.color = UIColor.red() // set red color to substrate node

Change images

  joystick.stick.image = UIImage(imageNamed: "yourStickImage") // set image to stick node
  joystick.substrate.image = UIImage(imageNamed: "yourSubstrateImage") // set image to substrate node

License

The MIT License (MIT)

Copyright (c) 2015...2018 Dmitriy Mitrophanskiy

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.