LoginSignup
3
2

More than 5 years have passed since last update.

【Swift】MRProgress の利用方法について まとめ

Last updated at Posted at 2015-04-10

概要

iOSライブラリのMRProgress利用方法についてです。

【Swift】MRProgress の利用方法について1
【Swift】MRProgress の利用方法について2

カスタマイズ結果

私的には使いやすくなりました。
結果を表示したいだけなら一行だけでOKです。
progressが設定されないモードで、表示だけすればCheckmarkとCrossは画面をタップで
画面が触れるようになります。
progressが設定できるモードではCustomProgress.Instance.progressChangeが1になってタップすれば画面が触れるようになります。
それ以外のモード(Loadingがくるくる回るだけ)は画面をさわれないので、処理が終了した際のイベントハンドラにCustomProgress.Instance.mrprogress.dismiss(true)をコールしてください。(ためしてないけど)
確認ダイヤログ代わりに使っても良さそう。
ただちゃんとテストしてないので、なんかおかしなところがあれば教えて下さい。

文字の色とか装飾も変えられると思いますけど、そろそろこれに飽きてきました。
別のことしようかと思います。
ちょっと時間空けてから見た方が、自分のコードの汚さに気がつきますよね。
その時コメントとかも入れていこう・・・

使い方

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func btn_StartClick(sender: AnyObject) {
        //CustomProgressModelにあるプロパティが初期設定項目
        //daikuが必要ないと思ったプロパティはなし
        //コンストラクタはオーバーロードしてないので、使ってみて必要になったら変更
        var initVal = CustomProgressModel()

        //以下5パターン
        CustomProgress.Create(self.view,initVal: initVal,modeView: EnumModeView.UIProgressView)
        //CustomProgress.Create(self.view.window!,initVal: initVal,modeView: EnumModeView.UIActivityIndicatorView)
        //CustomProgress.Create(self.view,initVal: initVal,modeView: EnumModeView.MRCrossIconView)
        //CustomProgress.Create(self.view,initVal: initVal,modeView: EnumModeView.MRCheckmarkIconView)
        //CustomProgress.Create(self.view,initVal: initVal,modeView: EnumModeView.MRCircularProgressView)
    }

    @IBAction func tap_Click(sender: AnyObject) {
        if(CustomProgress.isEnabledProgress()){
            //progressバーの機能があるモードの場合はこのプロパティを変更するだけ
            //プロパティの監視イベントでdidSetにて画面が更新される
            CustomProgress.Instance.progressChange += 0.1
        }
    }

}

共通クラス

あんまりStaticばかり増やすのはどうかな、とは思いますが、便利です。

Common/Progress.swift
//

import Foundation
import MRProgress

enum EnumModeView{

    case MRActivityIndicatorView
    case MRCircularProgressView
    case UIProgressView
    case UIActivityIndicatorView
    case MRCheckmarkIconView
    case MRCrossIconView
    case None
}

/*
初期値オブジェクト
*/
class CustomProgressModel{

    var progress:Float = 0.0
    var title:String = "Loading..."
    var trackTintColor:UIColor = UIColor.whiteColor()
    var progressTintColor:UIColor = UIColor.blueColor()
    //** tdaiku add property
    var compliteTitle:String = "Complite!!"
}

class CustomProgress{
    //static init 用
    init(){
        Instance.progressChange = 0
        Instance.modeView = EnumModeView.None
        Instance.view = nil
    }

    //static struct
    struct Instance{
        static var mrprogress:MRProgressOverlayView = MRProgressOverlayView()
        static var modeView = EnumModeView.None
        static var view:UIView! = nil
        static var progressChange:Float = 0.0{
            didSet {
                if(CustomProgress.Instance.progressChange >= 1.0){
                    progressChange = 1.0
                    CustomProgress.Instance.mrprogress.titleLabelText = compliteTitle
                }
                mrprogress.setProgress(progressChange, animated: true)
            }
        }
        static var compliteTitle = "complite!!"
    }

    class func Create(view:UIView,initVal:CustomProgressModel,modeView:EnumModeView){

        Instance.mrprogress = MRProgressOverlayView()
        Instance.modeView = modeView
        Instance.view = nil
        Instance.compliteTitle = initVal.compliteTitle

        var mrprogress = Instance.mrprogress
        mrprogress.titleLabelText = initVal.title

        switch modeView{

        case .UIProgressView:
            mrprogress.mode = MRProgressOverlayViewMode.DeterminateHorizontalBar

            var progress = UIProgressView()
            progress.progressViewStyle = UIProgressViewStyle.Default
            progress.progress = initVal.progress
            progress.trackTintColor = initVal.trackTintColor
            progress.progressTintColor = initVal.progressTintColor
            mrprogress.modeView = progress

        case .UIActivityIndicatorView:
            mrprogress.mode = MRProgressOverlayViewMode.IndeterminateSmall
            var progress = UIActivityIndicatorView()
            progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
            mrprogress.modeView = progress

        case .MRCrossIconView:
            MRProgressOverlayView.showOverlayAddedTo(view,title:initVal.title,mode:MRProgressOverlayViewMode.Cross, animated: true);
            Instance.view = view
            return

        case .MRCheckmarkIconView:
            MRProgressOverlayView.showOverlayAddedTo(view,title:initVal.title,mode:MRProgressOverlayViewMode.Checkmark, animated: true);
            Instance.view = view
            return

        case .MRCircularProgressView:
            mrprogress.mode = MRProgressOverlayViewMode.DeterminateCircular
            mrprogress.setTintColor(initVal.progressTintColor)
            var progress = MRCircularProgressView()
            progress.animationDuration = 0.3
            //progress.mayStop = true
            progress.progress = initVal.progress
            mrprogress.modeView = progress
            break;

        case .MRActivityIndicatorView:
            break

        case .None:
            break
        }

        view.addSubview(mrprogress)
        mrprogress.show(true)

    }

    class func isEnabledProgress() -> Bool{
        if CustomProgress.Instance.modeView == EnumModeView.UIProgressView ||
           CustomProgress.Instance.modeView == EnumModeView.MRCircularProgressView{
            return true
        }else{
            return false
        }
    }

    private class func Get(progress:UIProgressView){

    }

}

/*
progress完了時にtouchBeginイベントが動作したらprogressを削除する
*/
extension MRProgressOverlayView{
    var complite:Bool{
        get{
            if(CustomProgress.isEnabledProgress()){
                if(CustomProgress.Instance.progressChange >= 1.0){
                    CustomProgress()
                    return true
                }else{
                    return false
                }
            }
            return true
        }
    }

    override public func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        switch CustomProgress.Instance.modeView{
        case .None,.UIActivityIndicatorView:
            break;
        case .MRCheckmarkIconView,.MRCrossIconView:
            MRProgressOverlayView.dismissAllOverlaysForView(CustomProgress.Instance.view, animated: true)
        case .UIProgressView,.MRCircularProgressView:
            if(CustomProgress.Instance.mrprogress.complite){
                CustomProgress.Instance.mrprogress.dismiss(true)
            }
        default:
            CustomProgress.Instance.mrprogress.dismiss(true)
        }

    }
}

まとめ

というわけであとは使いながらデバッグ、カスタマイズしていこうと思います。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2