2015-09-29 14 views

答えて

1

私はあなたが新しいタプティックエンジンについて話していると思います。

apple.comから:iPhone 6sは、タップエンジンから、画面上と微妙なタップの両方でリアルタイムのフィードバックを提供します。これらの反応は、あなたがディスプレイをどれくらい深く押しているかに対応しており、実行しているアクションと起こることが期待できることを知らせます。

私が実際に知っているように、そのための公開APIはありません。 プライベートAPI経由でタプティックフィードバックを実装するために私はthis tutorialしか見つけていません。

//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

プライベートAPIが動作しません。 – mayqiyue

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"); 
    } 
} 
  • また、カスタム・ロジックと異なる力レベル毎に異なる振動時間を設定してみてください。

例:UIFeedbackGeneratoriOSの10以降で

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

、触覚フィードバックを処理するための新しいパブリックAPIがあります。

let generator = UINotificationFeedbackGenerator() 
generator.notificationOccurred(.success) 

発電機を使用して「ウェイクアップ」が必要なフィードバックハードウェアによる2との間にわずかな遅延があるようにフィードバックを送信する前に、.prepare()を呼び出すことが示唆されています。これは、viewDidLoad()や、まもなくフィードバックが来ることを期待している場合に比較できるもので行うことができます。

は、新しいAPIの良い説明し、利用可能なフィードバックのために、このブログを参照してください。他の記事で概説したよう
https://www.hackingwithswift.com/example-code/uikit/how-to-generate-haptic-feedback-with-uifeedbackgenerator

iOSの9の場合と古い、あなたはAudioToolBoxを使用することができます。 (iPhone用6S)スイフトで

import AudioToolbox 

private let isDevice = TARGET_OS_SIMULATOR == 0 

func vibrate() { 
    if isDevice { 
     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) 
    } 
} 
+1

iOS 10の追加のハプティッククラス:[UIImpactFeedbackGenerator](https://developer.apple.com/documentation/uikit/uiimpactfeedbackgenerator)と[UISelectionFeedbackGenerator](https://developer.apple.com/documentation/uikit/uiselectionfeedbackgenerator) – bendytree

+0

@bendytree新しいサブクラスを追加していただき、ありがとうございます。私はいつもここで古い投稿を見ているわけではありません:) –

9

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+ためhere're例。

func is3dTouchAvailable(traitCollection: UITraitCollection) -> Bool { 
    return traitCollection.forceTouchCapability == UIForceTouchCapability.available 
} 

、その後、タッチイベントでは、それは

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 { 
     // ... 
    } 
} 

touch.forceとして利用できるようになります。ここに私のブログがあります:あなたはそれが最初に利用できるかどうかを検出する必要がある -

力touch用として

コードサンプルを使用したより詳細な例を示します。
http://www.mikitamanko.com/blog/2017/02/01/swift-how-to-use-3d-touch-introduction/

+0

1519-1521コードはここでもう適用されないようです。あなたは問題にぶつかり合ったのですか? @Mikita Manko –

1

さまざまなフィードバックタイプがあります。それぞれを試して、あなたのニーズに合ったものを見つけてください:

// 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() 
関連する問題