一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

iOS中震动反馈(UIFeedbackGenerator)与系统震动详解

时间:2018-08-30 编辑:猪哥 来源:一聚教程网

Taptic Engine

先了解一个概念——Taptic Engine

Taptic Engine 是苹果产品上推出的全新震动模块,该元件最早出现在 Apple Watch 中。iPhone 6s 和 iPhone 6s Plus 中,也同样内置了Taptic Engine,在设计上有所升级。

Taptic Engine 振动模块为 Apple Watch 以及 iPhone 6s、iPhone 7 提供了 Force Touch 以及 3D Touch,不同的屏幕操作,可以感受到不同的振动触觉效果,带来更好的用户体验。

震动反馈(UIFeedbackGenerator)

震动反馈是iOS 10之后出的新特性,相比于之前的系统震动

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)

要友好得多,没有声音,震动幅度适中,不需要设置里“响铃模式震动”打开。这也是Apple更推荐开发者使用的反馈震动。

e.g. Switch控件滑动,时钟里选时间滑动,产生的震动都是UIFeedbackGenerator特性的。

现在“震动反馈”的应用是非常广的 —— 下拉刷新;点击重要的Button;选择器等等。都可以加上反馈。

Apple文档(UIFeedbackGenerator)

//
// UIImpactFeedbackGenerator.h
// UIKit
//
// Copyright © 2016 Apple Inc. All rights reserved.
//

#import 

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

想要用震动反馈也特别简单:

UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
[feedBackGenertor impactOccurred];

注意: “UIImpactFeedbackGenerator' is only available on iOS 10.0 or newer”,使用的时候加上版本限制。**

手机 -- 设置 -- 声音与触感 -- 系统触感反馈(打开)

此前系统震动AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)

在iOS 10之前,系统震动采用的是震动+铃声的模式,目前看来是及其不友好的,首先震动略大,其次带声音,体验并不好。但这种的方式可以自定义音效。

Apple文档(AudioServicesPlaySystemSound)

#import  

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

注意:手机 -- 设置 -- 声音与触感 -- 响铃模式震动(打开)

热门栏目