스위프트를 사용하여 iPhone을 진동시키는 방법?
아이폰을 진동시켜야하지만 스위프트에서 어떻게해야할지 모르겠다. Objective-C에서는 다음과 같이 작성합니다.
import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
그러나 그것은 나를 위해 작동하지 않습니다.
간단한 예 :
import UIKit
import AudioToolbox
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
}
휴대 전화에로드하면 진동합니다. 원하는대로 함수 나 IBAction에 넣을 수 있습니다.
iPhone 7 또는 7 Plus의 iOS 10에서 다음을 시도하십시오.
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
다른 진동 유형 :
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)
진동에 대한 자세한 정보-http: //www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/
스위프트 4.2 업데이트
아래 코드를 프로젝트에 삽입하십시오.
용법
Vibration.success.vibrate()
소스 코드
import AVFoundation
import UIKit
enum Vibration {
case error
case success
case warning
case light
case medium
case heavy
case selection
case oldSchool
func vibrate() {
switch self {
case .error:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
case .success:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
case .warning:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.warning)
case .light:
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
case .medium:
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
case .heavy:
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
case .selection:
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
case .oldSchool:
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
}
}
}
들어 아이폰 OS 10.0+ 당신이 시도 할 수 UIFeedbackGenerator을
위의 간단한 viewController, 테스트 "단일 뷰 앱"에서 뷰 컨트롤러를 교체하십시오.
import UIKit
class ViewController: UIViewController {
var i = 0
override func viewDidLoad() {
super.viewDidLoad()
let btn = UIButton()
self.view.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.widthAnchor.constraint(equalToConstant: 160).isActive = true
btn.heightAnchor.constraint(equalToConstant: 160).isActive = true
btn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
btn.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
btn.setTitle("Tap me!", for: .normal)
btn.setTitleColor(UIColor.red, for: .normal)
btn.addTarget(self, action: #selector(tapped), for: .touchUpInside)
}
@objc func tapped() {
i += 1
print("Running \(i)")
switch i {
case 1:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
case 2:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
case 3:
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.warning)
case 4:
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
case 5:
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
case 6:
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
default:
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
i = 0
}
}
}
우리는 이것을 Xcode7.1에서 할 수 있습니다
import UIKit
import AudioToolbox
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
}
}
You can vibrate the phone using either AudioServices
or Haptic Feedback
.
// AudioServices
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
// Haptic Feedback
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
Checkout my open source framework Haptica, it supports both Haptic Feedback
, AudioServices
and unique vibrations patterns. Works on Swift 4.2, Xcode 10
Swift 4.2
if #available(iOS 10.0, *) {
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
UINotificationFeedbackGenerator available from iOS 10 and work with Haptic v2, we can check this:
let feedbackSupportLevel = UIDevice.current.value(forKey: "_feedbackSupportLevel") as? Int
if #available(iOS 10.0, *), let feedbackSupportLevel = feedbackSupportLevel, feedbackSupportLevel > 1 {
do { // 1
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
}
do { // or 2
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
}
} else {
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
import AudioToolbox
extension UIDevice {
static func vibrate() {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
}
Now you can just call UIDevice.vibrate()
as needed.
Here you will find all codes for each .caf and associated category :
https://github.com/TUNER88/iOSSystemSoundsLibrary
For example if you want a lighter vibration you can use the code 1003.
Good luck and have fun ;)
참고URL : https://stackoverflow.com/questions/26455880/how-to-make-iphone-vibrate-using-swift
'program tip' 카테고리의 다른 글
RecyclerView에서 match_parent 너비가 작동하지 않습니다 (0) | 2020.08.06 |
---|---|
UIImage를 가로로 뒤집는 방법? (0) | 2020.08.06 |
React 프로젝트에서“클래스를 함수로 호출 할 수 없습니다”얻기 (0) | 2020.08.06 |
NUnit 테스트 실행 순서 (0) | 2020.08.06 |
방랑자의 포트 포워딩이 작동하지 않음 (0) | 2020.08.06 |