本文主要是介绍iOS 按钮添加点击震动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 方法说明:
iOS10后系统提供了一套API来简单实现震动:
init时传入一个style定义好的枚举就可以实现不同的震动
typedef NS_ENUM(NSInteger, UIImpactFeedbackStyle) {UIImpactFeedbackStyleLight,UIImpactFeedbackStyleMedium,UIImpactFeedbackStyleHeavy,UIImpactFeedbackStyleSoft API_AVAILABLE(ios(13.0)),UIImpactFeedbackStyleRigid API_AVAILABLE(ios(13.0))
};// UIImpactFeedbackGenerator is used to give user feedback when an impact between UI elements occurs
UIKIT_EXTERN API_AVAILABLE(ios(10.0)) API_UNAVAILABLE(visionos) API_UNAVAILABLE(tvos, watchos) NS_SWIFT_UI_ACTOR
@interface UIImpactFeedbackGenerator : UIFeedbackGenerator- (instancetype)initWithStyle:(UIImpactFeedbackStyle)style;/// call when your UI element impacts something else
- (void)impactOccurred;/// call when your UI element impacts something else with a specific intensity [0.0, 1.0]
- (void)impactOccurredWithIntensity:(CGFloat)intensity API_AVAILABLE(ios(13.0));@end
2. 举例
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
[generator impactOccurred];
3. 还有一套API
在AudioTool.framework里还有一套API可以实现震动,这个系统适配多些
#import <AudioToolbox/AudioToolbox.h>
// 类似于老系统来短信的震动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
// 类似touch一次短震
AudioServicesPlaySystemSound(1519);
// 类似touch长按app图标时的震动
AudioServicesPlaySystemSound(1520);
// 类似关iPhone左侧物理静音键的震动
AudioServicesPlaySystemSound(1521);
这篇关于iOS 按钮添加点击震动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!