ios 9 iphone 6S播放触觉反馈或振动

Kha*_*inn 19 ios9 3dtouch

用户强行触摸后,我想像默认行为一样振动手机.

它是触觉吗?如果是这样,我该怎么办?

小智 14

Swift中的示例(适用于iPhone 6S)

import AudioToolbox

AudioServicesPlaySystemSound(1519) // Actuate `Peek` feedback (weak boom)
AudioServicesPlaySystemSound(1520) // Actuate `Pop` feedback (strong boom)
AudioServicesPlaySystemSound(1521) // Actuate `Nope` feedback (series of three weak booms)
Run Code Online (Sandbox Code Playgroud)

以防万一 - 这里是iPhone 7/7 +的例子.

至于力触摸 - 您需要先检测它是否可用:

func is3dTouchAvailable(traitCollection: UITraitCollection) -> Bool {
    return traitCollection.forceTouchCapability == UIForceTouchCapability.available
}
Run Code Online (Sandbox Code Playgroud)

然后在触摸事件中,它将作为touch.force提供

func touchMoved(touch: UITouch, toPoint pos: CGPoint) {
    let location = touch.location(in: self)
    let node = self.atPoint(location)

    //...
    if is3dTouchEnabled {
        bubble.setPressure(pressurePercent: touch.force / touch.maximumPossibleForce)
    } else {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的博客,其中包含代码示例的更详细示例:http:
//www.mikitamanko.com/blog/2017/02/01/swift-how-to-use-3d-touch-introduction/


Fre*_*orf 12

iOS 10开始,有一个新的公共API用于处理触觉反馈:UIFeedbackGenerator.

let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
Run Code Online (Sandbox Code Playgroud)

.prepare()由于反馈硬件需要"唤醒" ,因此建议在使用发生器之前调用并发送反馈,因为两者之间存在轻微延迟.viewDidLoad()如果您希望不久之后能够提供反馈,那么这可以在或类似的情况下完成.

有关新API和可用反馈的详细说明,请参阅此博客:https:
//www.hackingwithswift.com/example-code/uikit/how-to-generate-haptic-feedback-with-uifeedbackgenerator

对于iOS 9及更早版本,您可以使用其他帖子中概述的AudioToolBox.

import AudioToolbox

private let isDevice = TARGET_OS_SIMULATOR == 0

func vibrate() {
    if isDevice {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • iOS 10 中的其他触觉类:[UIImpactFeedbackGenerator](https://developer.apple.com/documentation/uikit/uiimpactfeedbackgenerator) 和 [UISelectionFeedbackGenerator](https://developer.apple.com/documentation/uikit/uiselectionfeedbackgenerator) (2认同)

Tim*_*ich 5

有不同的反馈类型。尝试每一项找出适合您需求的方法:

// 1, 2, 3
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
generator.notificationOccurred(.success)
generator.notificationOccurred(.warning)

// 4
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()

// 5
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()

// 6
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()

// 7
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
Run Code Online (Sandbox Code Playgroud)


and*_*ani 1

我认为您正在谈论新的 Taptic Engine。

\n\n

来自 apple.com:iPhone 6s 通过屏幕和Taptic Engine的细微敲击形式为您提供实时反馈。这些响应与您按下显示屏的深度相对应,它们让您知道您正在执行什么操作以及预期会发生什么。

\n\n

据我所知,实际上没有公共 API。\n我只找到了通过私有 API 实现 Taptic 反馈的教程。

\n\n
//ATTENTION: This is a private API, if you use this lines of code your app will be rejected\n\nid tapticEngine = [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"_tapticEngine") withObject:nil];\n[tapticEngine performSelector:NSSelectorFromString(@"actuateFeedback:") withObject:@(0)];\n
Run Code Online (Sandbox Code Playgroud)\n