美文网首页
Swift iPhone 震动反馈封装

Swift iPhone 震动反馈封装

作者: 冰霜海胆 | 来源:发表于2017-04-25 16:15 被阅读379次

从 iPhone 最初的普通长震模式,到 6s 时代的 Peek、Pop,以及现在的 iPhone 7 独有的特殊震动反馈,都进行了简单的封装。

Github: https://github.com/Zane6w/Feedback

import UIKit
import AVFoundation
import AudioToolbox


fileprivate var audioPlayer: AVAudioPlayer?

/// 震动反馈示例音乐
fileprivate func playSound(forResource: String) {
    let bundle = Bundle(identifier: "com.zhi.HapticFeedbackKit")
    let path = bundle?.path(forResource: "FeedbackSounds", ofType: "bundle")
    let fileBundle = Bundle(path: path!)
    
    let url = fileBundle?.url(forResource: forResource, withExtension: "mp4")
    
    audioPlayer = try? AVAudioPlayer(contentsOf: url!)
    audioPlayer?.play()
}

/// 触觉反馈 (适用于 iPhone 7、7 Plus 及其以上机型)
open class HapticFeedback {
    
    @available(iOS 10.0, *)
    public struct Notification {
        
        fileprivate static var generator: UINotificationFeedbackGenerator = {
            let generator = UINotificationFeedbackGenerator()
            generator.prepare()
            
            return generator
        }()
        
        public static func function() {
            print(generator)
        }
        
        public static func successSound() {
            playSound(forResource: "success")
        }
        
        public static func warningSound() {
            playSound(forResource: "warning")
        }
        
        public static func errorSound() {
            playSound(forResource: "error")
        }
        
        
        public static func success() {
            occurred(.success)
        }
        
        public static func warning() {
            occurred(.warning)
        }
        
        public static func error() {
            occurred(.error)
        }
        
        fileprivate static func occurred(_ notificationType: UINotificationFeedbackType) {
            generator.notificationOccurred(notificationType)
            generator.prepare()
        }
        
    }
    
    @available(iOS 10.0, *)
    public struct Impact {
        
        fileprivate static var generator: UIImpactFeedbackGenerator?
        
        
        public static func lightSound() {
            playSound(forResource: "impact_light")
        }
        
        public static func mediumSound() {
            playSound(forResource: "impact_medium")
        }
        
        public static func heavySound() {
            playSound(forResource: "impact_heavy")
        }
        
        
        public static func light() {
            impactOccurred(.light)
        }
        
        public static func medium() {
            impactOccurred(.medium)
        }
        
        public static func heavy() {
            impactOccurred(.heavy)
        }
        
        fileprivate static func impactOccurred(_ style: UIImpactFeedbackStyle) {
            generator = UIImpactFeedbackGenerator(style: style)
            generator?.prepare()
            generator?.impactOccurred()
        }
        
    }
    
    @available(iOS 10.0, *)
    public struct Selection {
        
        fileprivate static var generator: UISelectionFeedbackGenerator = {
            let generator = UISelectionFeedbackGenerator()
            generator.prepare()
            
            return generator
        }()
        
        
        public static func selectionSound() {
            playSound(forResource: "selection")
        }
        
        
        public static func selection() {
            generator.selectionChanged()
            generator.prepare()
        }
        
    }
    
    @available(iOS 9.0, *)
    open class func peek() {
        AudioServicesPlaySystemSound(1519)
    }
    
    @available(iOS 9.0, *)
    open class func pop() {
        AudioServicesPlaySystemSound(1520)
    }
    
    @available(iOS 9.0, *)
    open class func error() {
        AudioServicesPlaySystemSound(1521)
    }
    
    open class func vibration() {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
    }
    
}

相关文章

  • Swift iPhone 震动反馈封装

    从 iPhone 最初的普通长震模式,到 6s 时代的 Peek、Pop,以及现在的 iPhone 7 独有的特殊...

  • 系统震动or震动反馈

    一.系统震动 1.AudioServicesPlaySystemSound() 系统震动是在iOS10.0之前存在...

  • iOS 震动反馈(UIFeedbackGenerator)和系统

    震动反馈(UIFeedbackGenerator) 震动反馈是iOS 10之后出的新特性,相比于之前的系统震动Au...

  • iOS 震动反馈

    http://zhoulingyu.com/2017/01/16/iOS——关于-Taptic-Engine-震动反馈/

  • iOS震动反馈

    长震动 支持iPhone基本机型,支持iOS9注:用户在“系统设置”中关闭了振动功能,此震动会失效。 短震动一 支...

  • iOS震动反馈

    之前的手机系统要用到的震动接口是AudioServicesPlaySystemSound,但这个震动幅度比较大,如...

  • Android震动反馈

    Android手机在输入法的时候可以设置一个轻震动反馈,这个反馈也可以用接口调用到需要用到震动反馈的地方。 这个震...

  • 记录一个荒谬的错误

    tags: swift 4.2, 日常爬坑 发现问题 最近在做类似手机来电响铃且震动功能。在一个网上找到一份封装播...

  • 按钮点击声音

    系统不一致,震动可能就不存在了 1.引入库 2.iphone6震动可大了 iphone7 没有震动 3.必须在ip...

  • Swift 运用协议泛型封装网络层

    Swift 运用协议泛型封装网络层 Swift 运用协议泛型封装网络层

网友评论

      本文标题:Swift iPhone 震动反馈封装

      本文链接:https://www.haomeiwen.com/subject/qgbvzttx.html