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];
相关推荐
Magnetic_h16 小时前
【iOS】单例模式
笔记·学习·ui·ios·单例模式·objective-c
归辞...18 小时前
「iOS」——单例模式
ios·单例模式·cocoa
yanling202320 小时前
黑神话悟空mac可以玩吗
macos·ios·crossove·crossove24
归辞...21 小时前
「iOS」viewController的生命周期
ios·cocoa·xcode
crasowas1 天前
Flutter问题记录 - 适配Xcode 16和iOS 18
flutter·ios·xcode
2401_852403551 天前
Mac导入iPhone的照片怎么删除?快速方法讲解
macos·ios·iphone
SchneeDuan1 天前
iOS六大设计原则&&设计模式
ios·设计模式·cocoa·设计原则
JohnsonXin2 天前
【兼容性记录】video标签在 IOS 和 安卓中的问题
android·前端·css·ios·h5·兼容性
蒙娜丽宁2 天前
Go语言错误处理详解
ios·golang·go·xcode·go1.19
名字不要太长 像我这样就好2 天前
【iOS】push和pop、present和dismiss
学习·macos·ios·objective-c·cocoa