活动指示器ActivityIndicatorView和进度条ProgressView

来源:互联网 发布:od软件 编辑:程序博客网 时间:2024/06/11 02:26

ActivityIndicatorView可以消除用户的心理等待时间,ProgressView可以指示请求的进度,也有消除用户心理等待时间的作用。


其中在ActivityIndicatorView中,重要的方法:isAnimating方法用于判断ActivityIndicatorView是否处于运动状态,stopAnimating方法用于停止旋转,startAnimating方法用于开始旋转。

进度条中用到了定时器NSTimer的方法scheduledTimerWithTimeInterval:(NSTimeInterval)seconds  target:(id)target  selector:(SEL)aSelector  userInfo:(id)userInfo  repeats:(bool)repeats,其中second参数设置时间间隔,target用于指定发送消息给那个对象,aSelector指定要调用的方法名,相当于一个函数指针,userInfo可以给消息发送参数,repeats表示是否重复。

实例代码如下:

//

//  ViewController.h

//  1006ActivityIndicatorViewAndProgressViewSample

//

//  Created by weibiao on 15-10-6.

//  Copyright (c) 2015 weibiao. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController {

    NSTimer *myTimer;

}

//活动只是其的输出口

@property (weak, nonatomic) IBOutletUIActivityIndicatorView *activityIndicator;

//活动指示器的方法

- (IBAction)upload:(id)sender;

//进度条的输出口

@property (weak, nonatomic) IBOutletUIProgressView *myProgressView;

//进度条的方法

- (IBAction)download:(id)sender;


@end


//

//  ViewController.m

//  1006ActivityIndicatorViewAndProgressViewSample

//

//  Created by weibiao on 15-10-6.

//  Copyright (c) 2015 weibiao. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()<UIAlertViewDelegate>


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)upload:(id)sender {

    if ([self.activityIndicatorisAnimating]) {

        [self.activityIndicatorstopAnimating];

    } else {

        [self.activityIndicatorstartAnimating];

    }

}

- (IBAction)download:(id)sender {

    myTimer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(download)userInfo:nilrepeats:YES];

}

- (void)download {

    self.myProgressView.progress =self.myProgressView.progress +0.1;

    if (self.myProgressView.progress ==1.0) {

        [myTimer invalidate];

        UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"download complete"message:@""delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];

        [alert show];

    }

}

@end



0 0
原创粉丝点击