iOS震动反馈
iOS 10之后,推出了震动反馈,比之前的震动效果好很多,现在在很多APP中的一些功能中会加入这个功能,让客户更明确自己的行为,比如在下拉刷新,在点击按钮获取验证码的时候,如果加上这个效果,会让用户体验更好一些。
震动反馈会用到 UIFeedbackGenerator 点击进入这个类,这类里面是这样的。
#import <UIKit/UIFeedbackGenerator.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, UIImpactFeedbackStyle) {
UIImpactFeedbackStyleLight,
UIImpactFeedbackStyleMedium,
UIImpactFeedbackStyleHeavy
};
// UIImpactFeedbackGenerator is used to give user feedback when an impact between UI elements occurs
UIKIT_CLASS_AVAILABLE_IOS_ONLY(10_0) @interface UIImpactFeedbackGenerator : UIFeedbackGenerator
- (instancetype)initWithStyle:(UIImpactFeedbackStyle)style;
/// call when your UI element impacts something else
- (void)impactOccurred;
@end
NS_ASSUME_NONNULL_END
#else
#import <UIKitCore/UIImpactFeedbackGenerator.h>
#endif
调用方法也很简单:
// 主线程
dispatch_async(dispatch_get_main_queue(), ^{
// 版本限制
if (@available(iOS 10.0, *)) {
// 初始化震动反馈类
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
// 准备
[generator prepare];
// 调用
[generator impactOccurred];
}
});
只需要在比如按钮点击的时候调用这个方法即可,很不多的用户体验