Watch OS 2 中 Taptic Engine 的开发教程

本文介绍如何在WatchOS2中使用Taptic Engine实现不同类型的触觉反馈,包括核心代码展示及实例演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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.运行你的应用吧


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值