67

iOS 震动反馈(UIFeedbackGenerator)和系统震动

 5 years ago
source link: http://www.cocoachina.com/ios/20180823/24673.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

震动反馈(UIFeedbackGenerator)

MjyY3mr.png!web

闹钟滑动时间的震动反馈

震动反馈是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”,使用的时候加上版本限制。**

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

zeaieyE.png!web

系统触感反馈

此前系统震动AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)

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

#import 

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

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

IZNjEbu.png!web

响铃模式震动

作者:whbalzac

链接:https://www.jianshu.com/p/ef7eadfae188


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK