swift 4.2实现活动指示器和进度条

代码:

import UIKit

class ViewController: UIViewController {
    
    var activityIndicatorView: UIActivityIndicatorView!
    
    var progressView: UIProgressView!
    var timer: Timer!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.view.backgroundColor = UIColor.black
        
        let screen = UIScreen.main.bounds
        
        //1. 获得指示器
        self.activityIndicatorView = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.whiteLarge)
        var frame = self.activityIndicatorView.frame
        frame.origin = CGPoint(x: (screen.size.width - frame.size.width)/2, y: 84)
        //重新设置控件位置
        self.activityIndicatorView.frame = frame
        
        //false:当控件处于非活动状态时,控件不会隐藏
        self.activityIndicatorView.hidesWhenStopped = false
        self.view.addSubview(self.activityIndicatorView)
        
        //2.Upload按钮
        let buttonUpload = UIButton(type: UIButton.ButtonType.system)
        buttonUpload.setTitle("Upload", for: UIControl.State())
        
        
        let buttonUploadWidth: CGFloat = 50
        let buttonUploadHeight: CGFloat = 30
        let buttonUploadTopView: CGFloat = 190
        
        buttonUpload.frame = CGRect(x: (screen.size.width - buttonUploadWidth)/2, y: buttonUploadTopView, width: buttonUploadWidth, height: buttonUploadHeight)

        buttonUpload.addTarget(self,action: #selector(startToMove(sender:)),for: .touchUpInside)
        self.view.addSubview(buttonUpload)
        
        //3.进度条
        let progressViewWidth:CGFloat = 200
        let progressViewHeight:CGFloat = 2
        let progressViewTopView: CGFloat = 283
        
        self.progressView = UIProgressView(frame: CGRect(x: CGFloat(screen.size.width - progressViewWidth)/2, y: progressViewTopView, width: progressViewWidth, height: progressViewHeight))
        
        self.view.addSubview(self.progressView)
        
        
        //4.Download按钮
        let buttonDownload = UIButton(type: UIButton.ButtonType.system)
        buttonDownload.setTitle("Download", for: UIControl.State())
        
        
        let buttonDownloadWidth: CGFloat = 69
        let buttonDownloadHeight: CGFloat = 30
        let buttonDownloadTopView: CGFloat = 384
        
        buttonDownload.frame = CGRect(x: (screen.size.width - buttonDownloadWidth)/2, y: buttonDownloadTopView, width: buttonDownloadWidth, height: buttonDownloadHeight)
        
        buttonDownload.addTarget(self,action: #selector(downloadProgress(_ :)),for: .touchUpInside)
        self.view.addSubview(buttonDownload)
        
    }
    
    @objc func startToMove(sender: AnyObject) {
        if(self.activityIndicatorView.isAnimating){
            self.activityIndicatorView.stopAnimating()
        } else {
            self.activityIndicatorView.startAnimating()
        }
    }
    
    @objc func downloadProgress(_ sender: AnyObject){
        self.timer = Timer.scheduledTimer(timeInterval: 1.0 , target: self, selector: #selector(download), userInfo: nil, repeats: true)
        
    }
    
    @objc func download(){
        self.progressView.progress = self.progressView.progress + 0.1
        if (self.progressView.progress == 1.0){
            self.timer.invalidate()
        
        
        let alertcontroller: UIAlertController =  UIAlertController(title: "download completed!", message: "", preferredStyle: UIAlertController.Style.alert)
        let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
        alertcontroller.addAction(okAction)
        //显示
        self.present(alertcontroller,animated: true, completion: nil)
        }
    }
    


}
image.png
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
源码iOS-ViewPagerIndicator,Paging indicator widgets,swift版的分页指示器,可以配合UIScrollView使用完成分页控制,Paging indicator widgets,swift版的分页指示器,可以配合UIScrollView使用完成分页控制 //样式设置 viewPagerIndicator.setTitleColorForState(UIColor.blackColor(), state: UIControlState.Selected)//选中文字的颜色 viewPagerIndicator.setTitleColorForState(UIColor.blackColor(), state: UIControlState.Normal)//正常文字颜色 viewPagerIndicator.tintColor = UIColor.brownColor()//指示器和基线的颜色 viewPagerIndicator.showBottomLine = false//基线是否显示 viewPagerIndicator.autoAdjustSelectionIndicatorWidth = true//指示器宽度是按照文字内容大小还是按照count数量平分屏幕 viewPagerIndicator.indicatorDirection = .Top//指示器位置 viewPagerIndicator.indicatorHeight = viewPagerIndicator.bounds.height//指示器高度 //点击viewPagerIndicator可以控制scrollView //滑动scrollView可以改变viewPagerIndicator //点击顶部选中后回调 func indicatorChange(indicatorIndex: Int){ scrollView.scrollRectToVisible(CGRectMake(self.view.bounds.width * CGFloat(indicatorIndex), 0, self.view.bounds.width, scrollViewHeight), animated: true) } //滑动scrollview回调 func scrollViewDidEndDecelerating(scrollView: UIScrollView) { var xOffset: CGFloat = scrollView.contentOffset.x var x: Float = Float(xOffset) var width:Float = Float(self.view.bounds.width) let index = Int((x + (width * 0.5)) / width) viewPagerIndicator.setSelectedIndex(index)//改变顶部选中 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值