0
点赞
收藏
分享

微信扫一扫

ios的震动反馈

最近写 ios TableView 加入长按的手势后,希望在 cell 长按弹窗的同时加入震动反馈效果。

查询后发现一个可用的震动方法:

1.导入 #import<AudioToolbox/AudioToolbox.h>

2.调用 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

这样就可以实现震动。

但震动很暴力,力度大,时间大概1s。感受很差。个人觉得像 twitter 上点赞那种震动感觉,或者像 iphone7 home 键的震动反馈,或者系统自带的 DatePicker 翻动时的触感都很棒。


后来发现上面函数可以传入其他参数来达到轻微震动效果:


// 普通短震,3D Touch 中 Peek 震动反馈
AudioServicesPlaySystemSound(1519);
// 普通短震,3D Touch 中 Pop 震动反馈
AudioServicesPlaySystemSound(1520);
// 连续三次短震
AudioServicesPlaySystemSound(1521);

注意:这里调用的是非公开 API。


另外 ios10 后加入的 UIImpactFeedbackGenerator ,提供了更好的震动效果。

调用也很简单:


UIImpactFeedbackGenerator*impactLight = [[UIImpactFeedbackGeneratoralloc]initWithStyle:UIImpactFeedbackStyleLight]; 
[impactLight impactOccurred];

震动有多个模式可选。


typedefNS_ENUM(NSInteger, UIImpactFeedbackStyle) {

    UIImpactFeedbackStyleLight,

    UIImpactFeedbackStyleMedium,

    UIImpactFeedbackStyleHeavy

};

注意:UIImpactFeedbackGenerator 只在 iphone7 后手机才会产生震动。

举报

相关推荐

0 条评论