SwiftSticks 1.0.2

SwiftSticks 1.0.2

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2017年9月
SwiftSwift 版本3.0
SPM支持 SPM

Damouse 维护。



  • 作者
  • Mickey Barboi

SwiftSticks

用 Swift 编写的 iOS 模拟摇杆。

此仓库包含了一个示例应用以及 Cocoapod。要快速试用 SwiftSticks,请克隆此仓库,运行 pod install 并运行应用目标。

设置

对于 Cocoapods,将以下内容添加到您的 Podfile 中

pod 'SwiftSticks'

使用 pod install 安装。

此代码库还包含一个快速演示应用。克隆仓库,打开项目,并运行 SwiftSticks-Example 目标。详见主 ViewController 中的示例实现。

使用方法

在此项目中有一个主要的类,即 StickView 类。您可以按照常规方式将其添加到视图中 – 在 InterfaceBuilder 中添加一个 StickView 子类,或按程序创建一个实例

import SwiftSticks

...

// Abbreviated example
let sticks = StickView(frame: someFrame)
self.view.addSubview(sticks)

摇杆的实际渲染,它是一个 SpriteKitNode,将始终根据 StickView 的大小初始化。请注意,目前不支持重新大小调整。

API

代理处理程序

@IBOutlet weak var stickView: StickView!

...

// Called as the user drags the stick around
stickView.didMove = { (a: AnalogJoystickData) -> () in
    print(a)
}

// Called when the user first touches the joystick and starts dragging it around
stickView.startedMoving = {
    print("Stick started moving")
}

// Called when the user stops touching the stick
stickView.stoppedMoving = {
    print("Stopped moving")
}

位置数据以 AnalogJoystickData 结构体返回,它包含两个字段: positionangle。位置记录从 -1+1 沿 x 和 y 轴。 angle 以弧度为单位测量,从 12 点位置开始,逆时针增加为正,顺时针增加为负。我没有设置库的这一部分,我也不知道为什么这样做,所以如果这让你很困扰,请提交一个 PR。

数据

stickView.data // => returns currentAnalogJoystickData  

stickView.isMoving // => true if the user is currently dragging the joystick

自定义

// Turn sticks on or off
stickView.disabled = true 

// Set the color of the stick or the base
stickView.stickColor = UIColor.white()
stickView.baseColor = UIColor.black()

// Use images for the stick
stickView.stickImage = UIImage(named: "YourImage")
stickView.baseImage = UIImage(named: "YourOtherImage")

其他

作为 MitrophD 的 此项目 的分支开始。

待办事项

  • 为摇杆视图创建一个 IBAction 代理
  • 适当地重构处理程序
  • 在 Stickview 和 SKView 之间添加约束以适当调整大小