美文网首页
iOS 仿系统音量(或亮度)调节条,自定义UISlider

iOS 仿系统音量(或亮度)调节条,自定义UISlider

作者: 微风_10a5 | 来源:发表于2022-05-28 23:27 被阅读0次

想做出与iOS系统音量调节条的效果,可以顺畅地滑动,调节百分比,结果在网上找轮子,没有找到合适的。只好自己动手撸一个
最终效果如下:

voiceBar.gif
  • 封装一个自定义的View,满足需求
  • 最好水平方向,竖直方向都能使用
  • 当前进度要回调给外面使用
正题

封装一个类:MyCustomSliderView,有用到第三方常用布局库SnapKit,做iOS的小伙伴,应该不会陌生

//
//  MyCustomSliderView.swift
//  MySliderDemo
//
//  Created by ZZ on 2022/5/28.
//

import UIKit
enum MyDirection {
    case horizontal
    case vertical
}
class MyCustomSliderView: UIView {
    var currentProgressBlock:((_ currentProgress:CGFloat)->Void)?
    
    var tempDirection : MyDirection = .horizontal
    var direction :MyDirection {
        get {
            return tempDirection
        }
        set {
            tempDirection = newValue
            foregroundView.snp.makeConstraints { make in
                make.leading.trailing.equalTo(self)
                make.bottom.equalTo(self.snp.bottom)
                make.top.equalTo(self.snp.bottom).offset(-self.progress*self.frame.size.height)
            }
            
        }
    }
    
    var tempProgress:CGFloat = 0.0
    var progress:CGFloat {
        get {
            return tempProgress
        }
        
        set {
            tempProgress = newValue
            if tempProgress > 1.0 {
                tempProgress = 1.0
            } else if tempProgress<0.0 {
                tempProgress = 0.0
            }
            if self.direction == .horizontal {
                foregroundView.snp.remakeConstraints { make in
                    make.top.bottom.equalTo(self)
                    make.leading.equalTo(self)
                    make.trailing.equalTo(self.snp.leading).offset(tempProgress*self.frame.size.width)
                }
            }else {
                foregroundView.snp.remakeConstraints { make in
                    make.leading.trailing.equalTo(self)
                    make.bottom.equalTo(self.snp.bottom)
                    make.top.equalTo(self.snp.bottom).offset(-tempProgress*self.frame.size.height)
                }
                
            }
        }
    }
    
    var tempMyBackgroundColor: UIColor = .lightGray
    var myBackgroundColor: UIColor {
        get{
            return tempMyBackgroundColor
        }
        set{
            tempMyBackgroundColor = newValue
            backgroundView.backgroundColor = tempMyBackgroundColor
        }
    }
    
    var tempForegroundColor: UIColor = .orange
    var foregroundColor: UIColor {
        get{
            return tempForegroundColor
        }
        set{
            tempForegroundColor = newValue
            foregroundView.backgroundColor = tempForegroundColor
        }
    }
    
    var tempCornerRadius:CGFloat = 10.0
    var cornerRadius: CGFloat  {
        get {
            return tempCornerRadius
        }
        set {
            tempCornerRadius = newValue
            self.backgroundView.layer.cornerRadius = newValue
            self.foregroundView.layer.cornerRadius = newValue
            
        }
    }
    private var backgroundView : UIView!
    private var foregroundView : UIView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
        backgroundView = UIView()
        backgroundView.backgroundColor = self.myBackgroundColor
        backgroundView.layer.cornerRadius = self.cornerRadius
        backgroundView.layer.masksToBounds = true
        let tapSingle = UIPanGestureRecognizer(target: self, action: #selector(pan(_:)))
        backgroundView.addGestureRecognizer(tapSingle)
        self.addSubview(backgroundView)
        backgroundView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.leading.equalToSuperview()
            make.trailing.equalToSuperview()
            
        }
        
        foregroundView = UIView()
        foregroundView.backgroundColor = self.foregroundColor
        foregroundView.isUserInteractionEnabled = false
        foregroundView.layer.cornerRadius = self.cornerRadius
        foregroundView.layer.masksToBounds = true
        self.addSubview(foregroundView)
        if self.direction == .horizontal {
            foregroundView.snp.makeConstraints { make in
                make.top.bottom.equalTo(self)
                make.leading.equalTo(self)
                make.trailing.equalTo(self.snp.leading).offset(self.progress*self.frame.size.width)
            }
        } else {
            foregroundView.snp.makeConstraints { make in
                make.leading.trailing.equalTo(self)
                make.bottom.equalTo(self.snp.bottom)
                make.top.equalTo(self.snp.bottom).offset(-self.progress*self.frame.size.height)
            }
        }
        
    }
    
    @objc func pan(_ cognizer:UIPanGestureRecognizer) {
        let location = cognizer.location(in: self)
        print("location x= \(location.x)")
        switch cognizer.state {
        case .began:
            
            break
            
        case .changed:
            
            let speed = cognizer.velocity(in: self)
            print("speed = \(speed)")
            
            if self.direction == .horizontal {
                let detal = speed.x/100.0
                self.progress += detal/100.0
                print("progress = \(self.progress)")
                if self.currentProgressBlock != nil {
                    self.currentProgressBlock!(progress)
                }
            }else {
                let detal = speed.y/100.0
                self.progress -= detal/100.0
                print("progress = \(self.progress)")
                if self.currentProgressBlock != nil {
                    self.currentProgressBlock!(progress)
                }
            }
            break
        case .ended:
            
            break
        default:
            break
            
            
        }
    }
    
}

有提供常用的属性

  • direction //当前方向
  • progress //初始时刻的进度
  • myBackgroundColor// 背景条的颜色
  • foregroundColor// 前景色
  • cornerRadius//控件的圆角
  • currentProgressBlock//一个闭包的回调
如何使用
//
//  ViewController.swift
//  MySliderDemo
//
//  Created by ZZ on 2022/5/27.
//

import UIKit

class ViewController: UIViewController {
    var progress = 0.0
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let label = UILabel()
        label.backgroundColor = .orange
        label.textAlignment = .center
        view.addSubview(label)
        label.snp.makeConstraints { make in
            make.centerX.equalTo(view.snp.centerX)
            make.width.equalTo(100.0)
            make.height.equalTo(50)
            make.top.equalTo(view.snp.top).offset(300.0)
        }
        let customView = MyCustomSliderView()
        customView.cornerRadius = 25.0
        customView.myBackgroundColor = .red
        view.addSubview(customView)
        customView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.equalTo(view).offset(20)
            make.trailing.equalTo(view).offset(-20)
            make.height.equalTo(50)
        }
        customView.currentProgressBlock = { progress in
            label.text = "\(Int(100.0*progress))%"
        }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.0001) {
            customView.progress = 0.5
            label.text = "\(Int(0.5*100.0))%"
        }
        
        
        let label2 = UILabel()
        label2.backgroundColor = .orange
        label2.textAlignment = .center
        view.addSubview(label2)
        label2.snp.makeConstraints { make in
            make.centerX.equalTo(view)
            make.top.equalTo(view.snp.centerY).offset(50)
            make.width.equalTo(100)
            make.height.equalTo(50)
        }
        
        let customView2 = MyCustomSliderView()
        customView2.foregroundColor = .purple
        customView2.direction = .vertical
        view.addSubview(customView2)
        customView2.snp.makeConstraints { make in
            make.centerX.equalTo(view)
            make.top.equalTo(view.snp.centerY).offset(150)
            make.width.equalTo(50)
            make.height.equalTo(200)
        }
        customView2.currentProgressBlock = { progress in
            label2.text = "\(Int(100.0*progress))%"
        }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.0001) {
            customView2.progress = 0.8
            label2.text = "\(Int(0.8*100.0))%"
        }

    }

}

结尾

2022年还在更新iOS技术文章,实属不易,小伴们,觉得有点用的话,或者已经看到这里面来的请点赞加关注吧~~
后续分享更多iOS原生技术及物联网技术相关文章。如果有任何疑问的话,欢迎在下方留言~

相关文章

  • iOS 仿系统音量(或亮度)调节条,自定义UISlider

    想做出与iOS系统音量调节条的效果,可以顺畅地滑动,调节百分比,结果在网上找轮子,没有找到合适的。只好自己动手撸一...

  • 系统音量 亮度调节

    调节系统的音量和亮度必须在真机环境中测试 调节音量 调节亮度

  • IOS UISlider,UIStepper

    UISlider滑块控件 //滑块控件 事件驱动型 应用场景:(调节音量、亮度等),高度34,固定不变 //设置最...

  • iOS系统音量&屏幕亮度调节

    一,系统音量获取 系统框架 获取系统音量slider 监听系统物理按键调节音量 获取系统当前音量 二,屏幕亮度 很...

  • iOS滑动条UISlider的使用方法 自定义和用滑块slide

    iOS滑动条UISlider的使用方法 iOS UISlider用法总结 自定义UISlider Iphone开发...

  • Android Setting大作战

    前言 本文讲的是Settings相关开发经常用到的地方,主要有WIFI、蓝牙、系统或应用升级、音量调节、亮度调节、...

  • 基于IJKPlayer框架的 iOS视频播放器

    这是一个基于ijkplayer框架的iOS视频播放器,包含全屏、播放、暂停、亮度调节、音量调节、进度条(边播边缓存...

  • iOS修改系统音量(MPVolumeView)

    iOS的AVFoundation调节音量大小时不会影响系统音量。但有时候我们可能希望修改系统音量,以免在调节声音的...

  • UISlider控件

    UISlider控件 UISlider控件,滑块控件(调节声音,屏幕亮度,播放进度,字体大小 滑块当前位置代表的数...

  • UISlider

    UIslider滑块控件在IOS开发中会常用到,可用于调节音量,字体大小等UI方面的交互, 用法总结如下: 初始化...

网友评论

      本文标题:iOS 仿系统音量(或亮度)调节条,自定义UISlider

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