iOS 9 iPhone 6S如何开启触感反馈或震动?

20

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

这是触觉反馈吗?如果是,我该怎么做?

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/


1519-1521的代码似乎不再适用于这里了。你遇到问题了吗?@Mikita Manko - Shai Mishali

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)
    }
}

2
iOS 10中的附加触觉类:UIImpactFeedbackGeneratorUISelectionFeedbackGenerator - bendytree
@bendytree,感谢您添加了新的子类,我并不总是在这里查看旧帖子 :) - Frederik Winkelsdorf

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()

1
我认为你在谈论新的触觉引擎。
来自apple.com: iPhone 6s通过Taptic引擎在屏幕和微妙的震动中实时反馈。这些反应对应您按下显示器的深度,并告诉您正在执行什么操作以及可以预期发生什么。
据我所知,实际上没有公共API可用于此。我只找到了this tutorial来通过私有API实现触觉反馈。
//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)];

0

您可以使用自定义逻辑来实现此操作:

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

例如:

- (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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接