19

用户强制触摸后,我想像默认行为一样振动手机。

是触觉的吗?如果是这样,我该怎么办?

4

5 回答 5

16

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/

于 2017-02-05T21:18:13.240 回答
13

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)
    }
}
于 2016-10-06T09:26:19.780 回答
6

有不同的反馈类型。尝试每一种,找出更适合您需求的方法:

// 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()
于 2017-06-27T11:15:17.913 回答
1

我认为您正在谈论新的 Taptic Engine。

来自 apple.com:iPhone 6s 以来自Taptic Engine的细微点击的形式为您提供屏幕上的实时反馈。这些响应与您按下显示屏的深度相对应,它们让您知道您正在执行哪些操作以及您可能会发生什么。

据我所知,实际上没有公共 API。我只发现本教程通过私有 API 实现 Taptic 反馈。

//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)];
于 2015-11-09T15:22:01.810 回答
0

您可以使用自定义逻辑来实现此目的:

  • force通过使用和类的maximumPossibleForce属性检测用户在屏幕上施加的力UITouch
  • 根据力的大小,您可以在特定级别后振动设备。

例子:

- (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);
}
于 2016-02-20T09:38:00.847 回答