通过UIPanGesture控制设备量

时间:2018-04-03 00:47:53

标签: ios swift uipangesturerecognizer notificationcenter

您好我试图通过UIPanGesture

控制设备量

我认为差不多已经完成但有一些错误

UIPanGesture状态结束后,音量指示器视图正在上下跳动

我认为他们仍会发布通知,因此我设置了isDragging变量

但仍然UIPanGesture发布了设备卷更改通知并仍在弹跳

这里的问题是我的代码

class MainVC: UIViewController {

lazy var sideVolumeIndicator:UIView = {
    let view = UIView()
    view.backgroundColor = .green
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

var volumeView:MPVolumeView = {
    let volumeView = MPVolumeView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    volumeView.isHidden = true
    return volumeView
}()

var isDragging:Bool = false

var currentOutputVolume:Float!
var sideVolumeHeightConstraint:NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    self.view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:))))
    setupSideVolumeIndicator()
    self.view.addSubview(volumeView)
}

func setupSideVolumeIndicator(){
    self.view.addSubview(sideVolumeIndicator)
    sideVolumeIndicator.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    sideVolumeIndicator.widthAnchor.constraint(equalToConstant: self.view.frame.width).isActive = true
    sideVolumeIndicator.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

    currentOutputVolume = AVAudioSession.sharedInstance().outputVolume
    let volumeHeightConstant = self.view.frame.height * CGFloat(currentOutputVolume)
    sideVolumeHeightConstraint = sideVolumeIndicator.heightAnchor.constraint(equalToConstant: volumeHeightConstant)
    sideVolumeHeightConstraint.isActive = true

}

@objc func handlePanGesture(_ gesture: UIPanGestureRecognizer) {
    if gesture.state == .began {
        isDragging = true
    }else if gesture.state == .changed {
        let velocity = gesture.velocity(in: self.view)
        guard let volumeView = volumeView.subviews.first as? UISlider else {return}

        if velocity.y > 0 {
            sideVolumeHeightConstraint.constant -= 2.5
        }else if velocity.y < 0 {
            sideVolumeHeightConstraint.constant += 2.5
        }

        if sideVolumeHeightConstraint.constant >= self.view.frame.height {
            sideVolumeHeightConstraint.constant = self.view.frame.height
        }else if sideVolumeHeightConstraint.constant <= 0 {
            sideVolumeHeightConstraint.constant = 0
        }
        volumeView.value = Float(sideVolumeHeightConstraint.constant / self.view.frame.height)
    }else if gesture.state == .ended {
        isDragging = false
    }
}

func setupCustomMpVolumeView(){
    let volumeView = MPVolumeView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
    volumeView.isHidden = true
    view.addSubview(volumeView)
}

@objc func volumeChanged(notification: NSNotification) {
    let volume = notification.userInfo!["AVSystemController_AudioVolumeNotificationParameter"] as! Float
    if isDragging == false{
        print("Only for device volume control button")
        sideVolumeHeightConstraint.constant = CGFloat(volume) * self.view.frame.height
    }
}
}

这是我的AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow()
    window?.makeKeyAndVisible()
    let rootVC = MainVC()
    NotificationCenter.default.addObserver(rootVC, selector: #selector(rootVC.volumeChanged(notification:)), name: Notification.Name("AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
    window?.rootViewController = rootVC
    return true
}
}

enter image description here

0 个答案:

没有答案