SubtleVolume 1.1.0

SubtleVolume 1.1.0

测试测试
语言语言 SwiftSwift
License MIT
发布最后发布2019年5月
SPM支持SPM

Andrea Mazzini 维护。



CocoaPods Carthage compatible Swift 5.0

当用户通过音量旋钮调整音量时,用一种更微妙的方式来显示音量弹出的音量。

为什么和如何

iOS默认显示的弹出音量状态,当用户点击音量旋钮时出现,它是一个很大的、反光的视图,覆盖了显示的内容。这个库提供了一种显示更微妙指示的方法。要确保不显示弹出窗口,需要满足两个条件:

  • AVAudioSession需要处于活动状态
  • MPVolumeView需要在当前视图的层次结构中,并且其alpha值需要大于0

当向您的视图中添加SubtleVolume后,音频会话会自动启动,视图的alpha值在隐藏状态下设置为0.0001

入门

使用它的便利初始化器之一创建SubtleVolume的实例,并设置其位置(您可以设置框架,或让自动布局来处理它)

let volume = SubtleVolume(style: .plain)
volume.frame = CGRect(x: 0, y: 10, width: UIScreen.main.bounds.width, height: 4) // or wherever you like

设置barTintColor属性

volume.barTintColor = .red

如有需要,设置动画类型(如果没有动画,则指示器将始终可见)

volume.animation = .slideDown

将视图添加到您的层次结构中

view.addSubview(volume)

通过程序更改音量

try? volume.setVolumeLevel(0.5)

或者使用便利方法

try? volume.decreaseVolume(by: 0.2, animated: true)
try? volume.increaseVolume(by: 0.2, animated: true)

附件图像

您可以为展示在酒吧左侧的附件图像提供。请参阅代理方法

func subtleVolume(_ subtleVolume: SubtleVolume, accessoryFor value: Double) -> UIImage? {
  return value > 0 ? #imageLiteral(resourceName: "volume-on.pdf") : #imageLiteral(resourceName: "volume-off.pdf")
}

iPhone X(S/R) 支持

想要在挖孔区域显示音量条?查看演示项目。主要要点如下

class ViewController: UIViewController {
  let volume = SubtleVolume(style: .rounded)
  var statusBarVisible = true

  // ...

  override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if view.safeAreaInsets.top > 0 {
      volume.padding = CGSize(width: 2, height: 8)
      volume.frame = CGRect(x: 16, y: 8, width: 60, height: 20)
    } else {
      // older phones here
    }
  }

  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }

  override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return .slide
  }

  override var prefersStatusBarHidden: Bool {
    return !statusBarVisible
  }
}

extension ViewController: SubtleVolumeDelegate {
  func subtleVolume(_ subtleVolume: SubtleVolume, didChange value: Double) {
    if !subtleVolume.isAnimating && view.safeAreaInsets.top > 0 {
      statusBarVisible = true
      UIView.animate(withDuration: 0.1) {
        self.setNeedsStatusBarAppearanceUpdate()
      }
    }
  }

  func subtleVolume(_ subtleVolume: SubtleVolume, willChange value: Double) {
    if !subtleVolume.isAnimating && view.safeAreaInsets.top > 0 {
      statusBarVisible = false
      UIView.animate(withDuration: 0.1) {
        self.setNeedsStatusBarAppearanceUpdate()
      }
    }
  }
}

处理后台状态

当您的应用进入后台时,您需要当它变得活跃时恢复会话

NotificationCenter.default.addObserver(volume, selector: #selector(SubtleVolume.resume), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

SubtleVolume 会自动在 deinit 时移除观察者

雇佣我们

Andrea Mazzini 编写。我们提供自由职业工作,欢迎联系 这里

想要支持这些免费库的开发?请买我一杯咖啡☕️通过 Paypal

MIT 许可证

Copyright (c) 2017-2018 Andrea Mazzini. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.