2015-09-29 7 views
14

उपयोगकर्ता बल को स्पर्श करने के बाद, मैं डिफ़ॉल्ट व्यवहार की तरह फ़ोन को कंपन करना चाहता हूं।आईओएस 9 आईफोन 6 एस प्ले हैप्टीक फीडबैक या कंपन

क्या यह हैप्टीक है? यदि हां, तो मैं कैसे करूँ?

उत्तर

1

मुझे लगता है कि आप नए टैप्टिक इंजन के बारे में बात कर रहे हैं।

apple.com से: iPhone 6s आप वास्तविक समय प्रतिक्रिया दोनों परदे पर और Taptic इंजन से सूक्ष्म नल के रूप में देता है। ये प्रतिक्रियाएं इस बात से मेल खाते हैं कि आप प्रदर्शन को कितनी गहराई से दबा रहे हैं, और वे आपको बताते हैं कि आप कौन सी कार्रवाइयां कर रहे हैं और आप क्या होने की उम्मीद कर सकते हैं।

जैसा कि मुझे पता है कि वास्तव में इसके लिए कोई सार्वजनिक एपीआई नहीं है। मुझे निजी एपीआई के माध्यम से टैप्टिक फीडबैक को लागू करने के लिए केवल 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 वर्ग के 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"); 
    } 
} 
  • इसके अलावा, आप कस्टम तर्क के साथ अलग अलग बल के स्तर के लिए अलग अलग कंपन अवधि सेट करने का प्रयास कर सकते हैं।

उदाहरण: UIFeedbackGenerator:

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

के साथ शुरू आईओएस 10, वहाँ हैप्टिक फ़ीडबैक से निपटने के लिए एक नया सार्वजनिक एपीआई है।

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

यह जनरेटर का उपयोग और के रूप में इन दोनों के बीच कुछ देरी प्रतिक्रिया हार्डवेयर एक "वेकअप" की आवश्यकता होती है की वजह से है फ़ीडबैक भेजने से पहले .prepare() कॉल करने के लिए सुझाव दिया है। यह viewDidLoad() में किया जा सकता है या कुछ तुलनीय है यदि आप शीघ्र ही बाद में प्रतिक्रिया देने की उम्मीद कर रहे हैं।

नई एपीआई का एक अच्छा विवरण और उपलब्ध फीडबैक के लिए इस ब्लॉग देखें:
https://www.hackingwithswift.com/example-code/uikit/how-to-generate-haptic-feedback-with-uifeedbackgenerator

आईओएस 9 और पुराने, आप AudioToolBox इस्तेमाल कर सकते हैं के रूप में अन्य पदों में उल्लिखित। स्विफ्ट में

import AudioToolbox 

private let isDevice = TARGET_OS_SIMULATOR == 0 

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

आईओएस 10 में अतिरिक्त हैप्टीक कक्षाएं: [UIImpactFeedbackGenerator] (https://developer.apple.com/documentation/uikit/uiimpactfeedbackgenerator) और [UISelectionFeedbackGenerator] (https://developer.apple.com/documentation/uikit/uiselectionfeedbackgenerator) – bendytree

+0

@bendytree नए उप-वर्ग जोड़ने के लिए धन्यवाद, मुझे हमेशा पुरानी पोस्ट पर नज़र डालना नहीं है :) –

9

उदाहरण (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+ के लिए 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 के रूप में उपलब्ध हो जाएगा कोड नमूने के साथ एक और विस्तृत उदाहरण के साथ:
http://www.mikitamanko.com/blog/2017/02/01/swift-how-to-use-3d-touch-introduction/

+0

1519-1521 कोड अब और लागू नहीं होते हैं। क्या आप मुद्दों में टक्कर लगी? @ मिकीता मांको –

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() 
संबंधित मुद्दे