gpt4 book ai didi

ios - 如何像应用程序商店安装按钮 Action 一样产生小振动?

转载 作者:行者123 更新时间:2023-11-28 23:53:59 24 4
gpt4 key购买 nike

当我们在应用商店中按下安装按钮时,我正在尝试发出小振动提醒。我正在使用下面的代码,但这会产生长时间的振动,我需要与应用商店完全相同的代码。许多应用程序以多种方式使用此功能,例如在 Collection View 单元格中长按时,它们会产生振动。

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

最佳答案

我使用以下代码在我的应用程序中实现了相同的要求。我相信它对您有用。

iOS 10 及更高版本您可以使用新的公共(public) API 来处理触觉反馈:UIFeedbackGenerator

let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
generator.notificationOccurred(.success)
generator.notificationOccurred(.warning)

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

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

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

// Update: Call this method for vibration
// Update: it is mandetory method for vibration

generator.prepare()

对于 iOS 9 及更早版本,您可以使用 AudioToolBox。

import AudioToolbox

private let isDevice = TARGET_OS_SIMULATOR == 0

func vibrate() {
if isDevice {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
}

关于ios - 如何像应用程序商店安装按钮 Action 一样产生小振动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51625241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com