haptics는 스마트 디바이스나 게임기 등에서 사용하는 휴먼 인터페이스 장치 중 하나로, 사용자에게 촉각과 운동감, 힘을 느끼게 해주는 기술

 

사용 방법은 간단합니다. UINotificationFeedbackGenerator에서 FeedbackType 또는 FeedbackStyle을 사용하는데 사용자한테 진동을 주는데 사용됩니다.

class HapticManager {
    static let instance = HapticManager() // 싱글톤
    
    func notification(type: UINotificationFeedbackGenerator.FeedbackType) {
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(type)
    }
    
    func impact(style: UIImpactFeedbackGenerator.FeedbackStyle) {
        let generator = UIImpactFeedbackGenerator(style: style)
        generator.impactOccurred()
    }
}

 

뷰에서는 다음과 같이 테스트할 수 있으며 직접 눌러가면서 차이를 비교하는 것이 제일 좋습니다.

struct ContentView: View {
    var body: some View {
        VStack(spacing: 20, content: {
            Button("error") { HapticManager.instance.notification(type: .error) }
            Button("success") { HapticManager.instance.notification(type: .success) }
            Button("warning") { HapticManager.instance.notification(type: .warning) }
            Divider()
            Button("soft") { HapticManager.instance.impact(style: .soft) }
            Button("heavy") { HapticManager.instance.impact(style: .heavy) }
            Button("medium") { HapticManager.instance.impact(style: .medium) }
            Button("rigid") { HapticManager.instance.impact(style: .rigid) }
            Button("light") { HapticManager.instance.impact(style: .light) }
        })
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ytw_developer