用户进行强制触摸后,我希望手机振动就像默认行为一样。
这是触觉反馈吗?如果是,我该怎么做?
在 Swift 中的示例(适用于 iPhone 6S)
import AudioToolbox
AudioServicesPlaySystemSound(1519) // Actuate `Peek` feedback (weak boom)
AudioServicesPlaySystemSound(1520) // Actuate `Pop` feedback (strong boom)
AudioServicesPlaySystemSound(1521) // Actuate `Nope` feedback (series of three weak booms)
以防万一-这里有关于 iPhone 7/7+ 的示例。
至于力触控,您需要首先检测它是否可用:
func is3dTouchAvailable(traitCollection: UITraitCollection) -> Bool {
return traitCollection.forceTouchCapability == UIForceTouchCapability.available
}
然后在触摸事件中,它将作为touch.force可用。
func touchMoved(touch: UITouch, toPoint pos: CGPoint) {
let location = touch.location(in: self)
let node = self.atPoint(location)
//...
if is3dTouchEnabled {
bubble.setPressure(pressurePercent: touch.force / touch.maximumPossibleForce)
} else {
// ...
}
}
这里是我的博客,提供了更详细的示例和代码:
http://www.mikitamanko.com/blog/2017/02/01/swift-how-to-use-3d-touch-introduction/
从iOS 10开始,有一个新的公共API用于处理触觉反馈:UIFeedbackGenerator
。
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
为了避免在使用生成器和发送反馈之间出现轻微的延迟,建议在调用.prepare()
后再使用它,并将此操作放在viewDidLoad()
或类似位置执行,如果您希望尽快给出反馈。
有关新API和可用反馈的详细说明,请参见此博客:
https://www.hackingwithswift.com/example-code/uikit/how-to-generate-haptic-feedback-with-uifeedbackgenerator
对于iOS 9及更早版本,您可以按照其他帖子中所述使用AudioToolBox。
import AudioToolbox
private let isDevice = TARGET_OS_SIMULATOR == 0
func vibrate() {
if isDevice {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
}
有不同类型的反馈。尝试每种类型来确定哪种更适合您的需要:
// 1, 2, 3
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
generator.notificationOccurred(.success)
generator.notificationOccurred(.warning)
// 4
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
// 5
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
// 6
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
// 7
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
//ATTENTION: This is a private API, if you use this lines of code your app will be rejected
id tapticEngine = [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"_tapticEngine") withObject:nil];
[tapticEngine performSelector:NSSelectorFromString(@"actuateFeedback:") withObject:@(0)];
您可以使用自定义逻辑来实现此操作:
UITouch
类的force
和maximumPossibleForce
属性,检测用户在屏幕上施加的力。例如:
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGFloat maximumPossibleForce = touch.maximumPossibleForce;
CGFloat force = touch.force;
CGFloat normalizedForce = force/maximumPossibleForce;
NSLog(@"Normalized force : %f", normalizedForce);
if (normalizedForce > 0.75)
{
NSLog(@"Strong");
// Vibrate device
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
else
{
NSLog(@"Weak");
}
}
示例:
// Vibrate device
NSTimer * vibrationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(vibrateDevice) userInfo:nil repeats:YES];
- (void) vibrateDevice
{
if(duration == 2) // duration is a public variable to count vibration duration
{
// Stop the device vibration
[vibrationTimer invalidate];
return;
}
duration++;
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}