iOS 添加震动效果

1. AudioServicesPlaySystemSound

较早的系统版本,我们会使用AudioTool.framework

objectivec 复制代码
#import <AudioToolbox/AudioToolbox.h>

一般震动

objectivec 复制代码
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

普通短震(类似3D Touch的 Peek 反馈 )

objectivec 复制代码
AudioServicesPlaySystemSound(1519);

普通短震 (类似3D Touch Pop 反馈)

objectivec 复制代码
AudioServicesPlaySystemSound(1520);

连续三次短震

objectivec 复制代码
AudioServicesPlaySystemSound(1521);

2. UIImpactFeedbackGenerator

iOS 10之后提供了UIImpactFeedbackGenerator

objectivec 复制代码
@interface UIImpactFeedbackGenerator : UIFeedbackGenerator

- (instancetype)initWithStyle:(UIImpactFeedbackStyle)style;

// 调用后开始震动
- (void)impactOccurred;

// 调用后开始震动,强度从0~1
- (void)impactOccurredWithIntensity:(CGFloat)intensity API_AVAILABLE(ios(13.0));

@end

UIImpactFeedbackStyle定义了震动的等级

objectivec 复制代码
typedef NS_ENUM(NSInteger, UIImpactFeedbackStyle) {
    UIImpactFeedbackStyleLight,
    UIImpactFeedbackStyleMedium,
    UIImpactFeedbackStyleHeavy,
    UIImpactFeedbackStyleSoft     API_AVAILABLE(ios(13.0)),
    UIImpactFeedbackStyleRigid    API_AVAILABLE(ios(13.0))
};

UIImpactFeedbackGenerator使用

objectivec 复制代码
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
[generator prepare];
[generator impactOccurred];
相关推荐
Digitally7 小时前
如何在Windows 10 PC上获取 iPhone短信
ios·iphone
脾气有点小暴8 小时前
uv-drop-down-popup 在 iOS 真机中随屏幕滚动偏移
ios·uniapp·uv
2501_924064118 小时前
2025年移动应用渗透测试流程方案及iOS与Android框架对比
android·ios
tangweiguo030519878 小时前
Objective-C 核心语法深度解析:基本类型、集合类与代码块实战指南
开发语言·ios·objective-c
代码s贝多芬的音符8 小时前
IOS webview打开相机 相册 选择文件上传
数码相机·ios
鹏多多1 天前
Flutter下拉刷新上拉加载侧拉刷新插件:easy_refresh全面使用指南
android·前端·ios
驱动探索者1 天前
[缩略语大全]之[IOS]篇
ios
kkk_皮蛋2 天前
在移动端使用 WebRTC (Android/iOS)
android·ios·webrtc
Swift社区2 天前
如何在 SwiftUI 中对 CoreImage 滤镜做实时预览
ios·swiftui·cocoa
ipad协议开发2 天前
企业微信iPad协议的开发进程
ios·企业微信·ipad