LoginSignup
4
4

More than 5 years have passed since last update.

CocoaPodの練習ついでにSVProgressHUDを試してみる

Last updated at Posted at 2015-03-13

ローディングマーク表示を楽にやりたかったんです。

遅まきながらCocoaPodsの恩恵にあやかりたいので導入して練習してみる。

既出ネタだけども、来週あたり使いそうなのでメモしておく。

SVProgressHUD | https://github.com/TransitApp/SVProgressHUD

platform :ios, "8.2"
pod 'SVProgressHUD'

これを書いたら
$ pod install
するだけでプロジェクトに追加とヘッダのサーチパスとかの設定とか諸々やってくれるこれは素晴らしい。

ローディングマークに加えてプログレス表示とかもできるようだったので、2パターン試してみたコードそのまま。

ViewController.m

#import "ViewController.h"
#import <SVProgressHUD.h>

#define PODTEST_LOADINGMARK

@interface ViewController ()
{
    BOOL    isLoadingShown;
    NSTimer *loadingTimer;
#ifdef PODTEST_LOADINGMARK
#else
    float progress;
#endif
}

-(IBAction)buttonPushed:(id)sender;
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    isLoadingShown = NO;

    [self resetTimer];
#ifdef PODTEST_LOADINGMARK
#else
    progress = 0.0f;
#endif
}

-(void)resetTimer {
    if (loadingTimer != nil) {
        [loadingTimer invalidate];
        loadingTimer = nil;
    }
}

-(void)onTimer:(NSTimer*)timer {
#ifdef PODTEST_LOADINGMARK
    isLoadingShown = NO;
    //    [SVProgressHUD dismiss];
    [SVProgressHUD showSuccessWithStatus:@"Time out"];
    [self resetTimer];
#else
    progress += 0.05;

    [SVProgressHUD showProgress:progress status:@"processing..."];
    if (progress >= 1.0) {
        isLoadingShown = NO;
//        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"Complete!"];
        progress = 0.0f;
        [self resetTimer];
    }
#endif
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)buttonPushed:(id)sender
{
    if (isLoadingShown) {
        isLoadingShown = NO;
        [self resetTimer];
        [SVProgressHUD dismiss];
#ifdef PODTEST_LOADINGMARK
#else
        progress = 0.0f;
#endif
    }
    else {
        isLoadingShown = YES;
#ifdef PODTEST_LOADINGMARK
        loadingTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
                                                        target:self
                                                      selector:@selector(onTimer:)
                                                      userInfo:nil
                                                       repeats:NO];

        // マーク表示中は操作可
        [SVProgressHUD showWithStatus:@"Loading..."];

        // マーク表示中は操作不可(3パターン)
//        [SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeClear];   // 消す時、dismissを合わせて呼ぶと、Successのメッセージ表示されないっぽい
//        [SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeGradient];
//        [SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeBlack];
#else
        loadingTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                        target:self
                                                      selector:@selector(onTimer:)
                                                      userInfo:nil
                                                       repeats:YES];
        [SVProgressHUD showProgress:progress];
#endif
    }
}

@end

<追記>
Progress表示にはNSTimerを使ったが、
ちょっと動きを試すとか、単純に一定時間カウントアップするだけなら、
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
を使えばよかった。
本家のDemoを見て使い方を知った次第。また一つ勉強になりました。
* SVProgressHUD/ViewController.m at master · TransitApp/SVProgressHUD

見慣れた感じのローディングマークとしてはこちらもよさそう。
* matej/MBProgressHUD

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