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];
相关推荐
择势5 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室 —— 常见问题汇总 & 解决方案手册
ios
择势5 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室(进阶封装)
ios
择势5 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室——从零到可跑的指南
ios
白玉cfc6 小时前
【iOS】底层原理:类的加载
ios·objective-c·xcode
光电的一只菜鸡8 小时前
shell脚本开发技巧
开发语言·ios·swift
2501_916007479 小时前
iOS应用性能优化全面指南:从内存管理到工具使用
android·ios·性能优化·小程序·uni-app·iphone·webview
库奇噜啦呼9 小时前
【iOS】源码学习-类的加载
学习·ios·cocoa
ayqy贾杰10 小时前
我同事,40了,他vibe coding了个App
前端·ios·客户端
吠品11 小时前
Ubuntu下grep配合管道用的几个场景
ios·notepad++·iphone