LoginSignup
6
6

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-04-09

概要

AvtivityIndicatorViewにプログレスバーの機能をつけたコントロールが欲しくてライブラリを探してました。
結果MRProgressにたどり着いたんですが、使い方がググっても全然わかりませんでした。
ライブラリもObjective-Cで書かれてるので読むのが難しい・・・

とりあえず試行錯誤しながら使い方を検証してます。
わかる方がいれば教えて下さい!
もしくは記事を投稿して下さい(ずうずうしい)

MRProgressについて

iOSのライブラリでSwiftでも利用可能です。
GitHub MRProgress
MRProgress パワフルなiOS7プログレスビューのコレクション

導入方法

swift対応バージョンのCocoaPodsを利用していれば、利用したいソースで

filename.swift
import MRProgress

taketin氏:Swift 対応版 CocoaPods を使う

基本用法

表示や停止をclass func で定義していて、表示と非表示はこんな感じ。
modeとtitleを指定しないoverloadもあります。

**
注viewDidLiad()内に記述したらアニメーションが動きませんでした。
**

filename.swift
//処理開始時
MRProgressOverlayView.showOverlayAddedTo(self.view,title:"",mode:MRProgressOverlayViewMode.DeterminateCircular, animated: true);

//もしくは以下で
MRProgressOverlayView.showOverlayAddedTo(self.view, animated: true);

//処理終了時のイベントハンドラ等に記述
MRProgressOverlayView.dismissOverlayForView(self.view, animated: true)

定義はこんな感じになってました。
設定できるmodeはMRProgressOverlayViewMode:Enumにある分です。

MRProgressOverlayView

import UIKit

//
//  MRProgressOverlayView.h
//  MRProgress
//
//  Created by Marius Rackwitz on 09.10.13.
//  Copyright (c) 2013 Marius Rackwitz. All rights reserved.
//

/** (MRProgressOverlayViewMode) */
enum MRProgressOverlayViewMode : UInt {
    case Indeterminate
    case DeterminateCircular
    case DeterminateHorizontalBar
    case IndeterminateSmall
    case IndeterminateSmallDefault
    case Checkmark
    case Cross
    case Custom
}

/**
 Progress HUD to be shown over a whole view controller's view or window.
 Similar look to UIAlertView.
 */
class MRProgressOverlayView : UIView {

    class func showOverlayAddedTo(view: UIView!, animated: Bool) -> Self!
    class func showOverlayAddedTo(view: UIView!, title: String!, mode: MRProgressOverlayViewMode, animated: Bool) -> Self!
    class func dismissOverlayForView(view: UIView!, animated: Bool) -> Bool
    class func dismissAllOverlaysForView(view: UIView!, animated: Bool) -> UInt
    class func overlayForView(view: UIView!) -> Self!
    class func allOverlaysForView(view: UIView!) -> [AnyObject]!
    func createBlurView() -> UIView!
    var mode: MRProgressOverlayViewMode
    var progress: Float
    var titleLabelText: String!
    var titleLabelAttributedText: NSAttributedString!
    var titleLabel: UILabel! { get }
    var modeView: UIView!
    func setTintColor(tintColor: UIColor!)
    func setProgress(progress: Float, animated: Bool)
    func show(animated: Bool)
    func hide(animated: Bool)
    func dismiss(animated: Bool)
}

ソースは確認したところこんな感じ。

MRProgressOverlayView.m
+ (instancetype)showOverlayAddedTo:(UIView *)view animated:(BOOL)animated {
   MRProgressOverlayView *overlayView = [self new];
   [view addSubview:overlayView];
   [overlayView show:animated];
   return overlayView;
}

+ (instancetype)showOverlayAddedTo:(UIView *)view title:(NSString *)title mode:(MRProgressOverlayViewMode)mode animated:(BOOL)animated {
    MRProgressOverlayView *overlayView = [self new];
    overlayView.mode = mode;
    overlayView.titleLabelText = title;
    [view addSubview:overlayView];
    [overlayView show:animated];
    return overlayView;
}

+ (BOOL)dismissOverlayForView:(UIView *)view animated:(BOOL)animated {
   MRProgressOverlayView *overlayView = [self overlayForView:view];
   if (overlayView) {
       [overlayView dismiss:animated];
       return YES;
   }
   return NO;
}

とりあえず

長くなりそうなのでこれくらいで記事を終わります。
そろそろ別の作業もしたいし、、、、
明日にでもカスタマイズの方法をまとめます。
正直わかりづらいので自分で使いやすいように変更します。

とはいってもObjective-Cのソースはいじりたくないので、MRProgressのラッパークラスを作成する感じ。
もしくはextensionしたほうがよければそれもあり。

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

6
6
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
6
6