iOS中自定义进度条设置半透明背景(Swift 3)
来源:互联网 发布:proxifier for linux 编辑:程序博客网 时间:2024/06/06 14:57
在显示进度条的时候,有时候会发现用自带的progressView可自定义的属性比较少,并不能满足某些需求,继承UIView重新实现一个也挺方便的,自定义progress view 代码如下:
import Foundationimport UIKitclass DOVProgressView: UIView { private let textLabel = UILabel() private let bar = UIView() public var progress: Float = 0{ didSet{ percent = Int(progress * 100) } } var percent: Int = 0 { didSet { if percent > 100 { percent = 100 }else if percent < 0 { percent = 0 } textLabel.text = "\(percent)%" setNeedsLayout() } } //文本颜色 var color: UIColor = UIColor.black { didSet { textLabel.textColor = color } } //进度条颜色 var barColor: UIColor = UIColor.orange { didSet { bar.backgroundColor = barColor } } //进度条背景颜色 var barBgColor: UIColor = UIColor.white { didSet { layer.backgroundColor = barBgColor.cgColor } } override init(frame: CGRect) { super.init(frame: frame) initialSetup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) initialSetup() } private func initialSetup(){ bar.backgroundColor = self.barColor addSubview(bar) self.layer.cornerRadius = CGFloat(12) self.layer.masksToBounds = true self.bar.layer.cornerRadius = CGFloat(12) self.bar.layer.masksToBounds = true textLabel.textAlignment = .center textLabel.numberOfLines = 0 textLabel.textColor = self.color textLabel.text = "\(self.percent)%" addSubview(textLabel) } override func layoutSubviews() { super.layoutSubviews() layer.backgroundColor = self.barBgColor.cgColor var barFrame = bounds barFrame.size.width *= (CGFloat(self.percent) / 100) bar.frame = barFrame textLabel.frame = bounds }}
自定义进度条后,想让进度条显示在一个半透明的背景上,这样的操作还是经常遇到的,可以直接在屏幕上加一个Window,然后把自定义的进度条显示在上面,代码如下:
import Foundationimport UIKitclass DOVUserInteractiveManager: NSObject { private var _boardWindow : UIWindow? static let shared : CXEUserInteractiveManager = { let instance = CXEUserInteractiveManager.init() return instance }() // MARK:load界面 var boardWindow : UIWindow { if(self._boardWindow == nil) { let window : UIWindow = UIWindow(frame: UIScreen.main.bounds) window.windowLevel = UIWindowLevelStatusBar window.backgroundColor = UIColor.gray.withAlphaComponent(0.7) window.isUserInteractionEnabled = true window.makeKeyAndVisible() self._boardWindow = window } return self._boardWindow! } public func diss() { self.boardWindow.isHidden = true self.boardWindow.removeFromSuperview() } public func show(_ view : UIView) { self.boardWindow.addSubview(view) self.boardWindow.isHidden = false }}
使用方法如下:
import UIKitclass ViewController: UIViewController { var timer:Timer? var progress:Float = 0 override func viewDidLoad() { super.viewDidLoad() let button = UIButton(type: .system) button.setTitle("点我点我点我", for: .normal) button.frame = CGRect(x:0, y:0, width:Int(UIScreen.main.bounds.width.native * 0.8), height:30) button.center = CGPoint(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY - 50) button.addTarget(self, action: #selector(self.onClike), for: .touchUpInside) self.view.addSubview(button) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func onClike(){ let progressView = CXEProgressView(frame: CGRect(x:0, y:0, width:Int(UIScreen.main.bounds.width.native * 0.8), height:22)) progressView.center = CGPoint(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY) DOVUserInteractiveManager.shared.show(progressView) let cancelProgressButton = UIButton(type: .system) cancelProgressButton.frame = CGRect(x: 0, y: 0, width: Int(UIScreen.main.bounds.width.native * 0.8), height: 60) cancelProgressButton.center = CGPoint(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY + 80) let attributes:[String:Any] = [NSFontAttributeName: UIFont.systemFont(ofSize: 15)] cancelProgressButton.setAttributedTitle(NSAttributedString(string:"你再点我试试", attributes: attributes), for: .normal) cancelProgressButton.tintColor = UIColor.white cancelProgressButton.addTarget(self, action: #selector(self.onCancleProgress), for: .touchUpInside) DOVUserInteractiveManager.shared.show(cancelProgressButton) timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true){_ in progressView.progress = self.progress self.progress += 0.1 } } func onCancleProgress(){ CXEUserInteractiveManager.shared.diss() self.timer?.invalidate() }}Contact GitHub API Training Shop Blog About
Demo下载:https://github.com/Willib/CustomProgress
效果如下:
0 0
- iOS中自定义进度条设置半透明背景(Swift 3)
- div设置背景半透明
- PopWindows背景半透明设置
- iOS中自定义步骤进度条
- tableview 背景设置为半透明
- 背景半透明CSS兼容设置
- sublime text 背景半透明设置
- 自定义进度条背景
- IOS-自定义navigationBar,设置背景方法:
- Ios开发Swift自定义UIbutton增加一个能设置高亮状态下背景颜色的方法
- css中设置半透明
- 设置 UINavigationBar 全局颜色,背景颜色,前景颜色 swift 3 4 ios 10 +
- 设置button背景的半透明效果!!!
- Android Activity背景半透明设置方法
- 关于设置半透明背景的一些技巧
- html通过css来设置半透明背景
- Android Alertdialog弹出框设置半透明背景
- Android背景设置透明和半透明效果
- Java DecimalFormat 用法 .
- 浅谈物联网的发展
- CodeForces 443C Borya and Hanabi
- Java中的流程控制
- 在Ubuntu上搭建redis服务器
- iOS中自定义进度条设置半透明背景(Swift 3)
- HDU-1002
- 分布式架构实战--FastDFS分布式文件系统集群安装
- gdb调试qemu本身
- C#解析JSON字符串总结
- Request对象的主要方法
- CCF 2016 9-1 最大波动
- 系统资源查看
- 第八周 mooc C 单词长度