【问题标题】:ios 9 iphone 6S Play Haptic feedback or vibrateios 9 iphone 6S 播放触觉反馈或振动
【发布时间】:2015-12-26 18:40:16
【问题描述】:

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

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

【问题讨论】:

    标签: ios9 3dtouch


    【解决方案1】:

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

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

    据我所知,实际上并没有公开的 API。 我只找到this tutorial 用于通过私有 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)];
    

    【讨论】:

      【解决方案2】:

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

      • 使用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);
      }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        【解决方案4】:

        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
        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-07-10
          • 1970-01-01
          • 1970-01-01
          • 2019-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多