您的位置:首页 > 移动开发

Watch OS 2 中 Taptic Engine 的开发教程

2015-07-20 19:01 405 查看
在WWDC2015上,Apple发布了Watch OS 2,增加了Taptic Engine的访问

先上核心代码

WKInterfaceDevice.currentDevice().playHaptic(type: WKHapticType)


超级简单是不是?就一句是不是?秒懂有木有!

那么我们来讲一讲最后一个参数

WKHapticType一共有9种

它们分别是

Notification  //这是在Apple Watch接受通知时的震动

DirectonUp  //上滑到头的震动

DirectionDown  //下滑到头的震动

Success  //成功返回的震动

Failure  //失败返回的震动

Retry  //表示重试的震动

Start  //表示开始的震动

Stop  //表示停止的震动

Click  //轻击的震动
高考水平的单词,大家应该都懂是什么意思,但造福人民,我还是注释一下

那么怎么用呢

我来举个例子

WKInterfaceDevice.currentDevice().playHaptic(.Click)

就是在这个语句执行后Apple Watch会用线性致动器轻击你的手腕

想记住它很简单:(Watch Kit Interface Device)Current Device Play Haptic

好,那么我们把它们带入到应用中去

1.新建一个watch os 2 project



2.打开watch的故事版



3.放置3个Group到ViewController上



4.设置所有group的height(在旁边的检查器)为relative to container并将数值写为0.33



5.拖入一个button,在同样的地方设置width的值为0.33,一组放三个,并起一个你喜欢的名字(能辨认出哪个是哪个即可)



6.在interface controller中加入以下代码

@IBAction func Notification()
{
WKInterfaceDevice.currentDevice().playHaptic(.Notification)
}
@IBAction func DirectionUp()
{
WKInterfaceDevice.currentDevice().playHaptic(.DirectionUp)
}
@IBAction func DirectionDown()
{
WKInterfaceDevice.currentDevice().playHaptic(.DirectionDown)
}
@IBAction func Success()
{
WKInterfaceDevice.currentDevice().playHaptic(.Success)
}
@IBAction func Failure()
{
WKInterfaceDevice.currentDevice().playHaptic(.Failure)
}
@IBAction func Retry()
{
WKInterfaceDevice.currentDevice().playHaptic(.Retry)
}
@IBAction func Start()
{
WKInterfaceDevice.currentDevice().playHaptic(.Start)
}
@IBAction func Stop()
{
WKInterfaceDevice.currentDevice().playHaptic(.Stop)
}
@IBAction func Click()
{
WKInterfaceDevice.currentDevice().playHaptic(.Click)
}


如图所示



7.然后在view controller里关联好ibaction



8.运行你的应用吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息