开发过程当中,有时候会碰到点击按钮或者某个动画会配合震动效果;
下面介绍iOS开发过程当中的震动添加:ios
导入:#import <AudioToolbox/AudioToolbox.h>iphone
在须要出发震动的地方写上代码:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//默认震动效果动画
若是想要其余震动效果,可参考:
// 普通短震,3D Touch 中 Pop 震动反馈
AudioServicesPlaySystemSound(1520);ui
// 普通短震,3D Touch 中 Peek 震动反馈
AudioServicesPlaySystemSound(1519);spa
// 连续三次短震
AudioServicesPlaySystemSound(1521);code
另外 ios10 后加入的 UIImpactFeedbackGenerator ,提供了更好的震动效果。ip
调用也很简单:开发
UIImpactFeedbackGenerator*impactLight = [[UIImpactFeedbackGeneratoralloc]initWithStyle:UIImpactFeedbackStyleLight]; [impactLight impactOccurred];
震动有多个模式可选。it
typedefNS_ENUM(NSInteger, UIImpactFeedbackStyle) { UIImpactFeedbackStyleLight, UIImpactFeedbackStyleMedium, UIImpactFeedbackStyleHeavy };
注意:UIImpactFeedbackGenerator 只在 iphone7 后手机才会产生震动。io
参考:
https://www.jianshu.com/p/064c225efb0f
https://www.jianshu.com/p/d1f5b4ec3a1d