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];
相关推荐
ITKEY_14 小时前
appstore上架-预览和截屏
ios·appstore
阿捏利14 小时前
详解Mach-O(三十三)Mach-O __mod_term_func节
macos·ios·c/c++·mach-o
2501_9160074716 小时前
提高开发效率的尝试,用快蝎(kxapp)完成 iOS 项目的创建、调试与构建
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
2501_9151063217 小时前
如何在 Mac 上面代理抓包和数据流分析
android·macos·ios·小程序·uni-app·iphone·webview
blackorbird17 小时前
Coruna 间谍软件活动持续扩散,苹果破例为旧版iOS设备推送双版本安全补丁
macos·ios·objective-c·cocoa
for_ever_love__18 小时前
Objective-C 学习 单例模式
学习·ios·单例模式·objective-c
cola_wh18 小时前
避免团队多人 cocopods 冲突未验证
ios
2501_9159214318 小时前
在 Linux 上通过命令行上架 iOS APP,Fastlane + AppUploader(开心上架)
android·linux·运维·ios·小程序·uni-app·iphone
2501_9159214318 小时前
从构建到 IPA 保护,Flutter iOS 包如何做混淆与安全处理
android·安全·flutter·ios·小程序·uni-app·iphone
2501_9160088918 小时前
iPhone 手机硬件组件使用耗能历史记录查看,能耗查看
android·ios·智能手机·小程序·uni-app·iphone·webview