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

想做出与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原生技术及物联网技术相关文章。如果有任何疑问的话,欢迎在下方留言~

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容