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];
相关推荐
福兮说7 小时前
浏览器里把 iPhone 的 HEIC 转成 JPG:Chrome 解不开、转完大了七成、拍摄时间全丢,六个坑实测
前端·javascript·图像处理·chrome·ios·iphone·heic
茶底世界之下14 小时前
时间线效果层,为什么不能只复用一个处理器?
ios·swift
ZZH_AI项目交付14 小时前
实时字幕负责快,本地录音负责不丢:一个 iOS 录音功能为什么需要两条链路
ios·ai编程
Cc.Y15 小时前
Swift深度链接的极简之道
ios·cocoa·swift
AI砖家16 小时前
iPhone Duo 上架新规:旧 App 怎么兼容,还能不能继续更新?
ios·cocoa·iphone·ios上架·ios上架新规
黑科技iOS上架16 小时前
小蟹iOS混淆:Objective-C项目实战
ios·审核·ios混淆·执行程序差异化
茶底世界之下17 小时前
照片边缘太抢眼?用 Awaking 做一次 1:1 裁切
ios
Digitally17 小时前
如何在iPhone上永久删除文件而无需恢复
ios·iphone
黑科技iOS上架18 小时前
iOS二进制混淆实战:工程级指纹防护彻底解决4.3同质化拒审
ios·审核·ios混淆·执行程序差异化
CocoaKier1 天前
苹果商店详情顶部头图已面向所有开发者开放!
ios·apple