NSTimer,UIActivityIndicatorView,UIProgressView等控件额使用方法

说明

本例子主要简示了和时间相关的一些控件的用法,模拟了一个下载器。

运行结果

主要代码

@synthesize _labelInfo;
@synthesize _textInfo;
@synthesize _buttonDownload;
@synthesize _processViewDownload;
@synthesize _activityIndicatorDownload;
@synthesize _sliderDownload;
@synthesize _switchDownload;
@synthesize _timerDownload;

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

    _imageBackground = [UIImage imageNamed:@"leaf.jpg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:_imageBackground];

    // 位置大小预设值
    CGFloat x = self.view.frame.size.width / 8;
    CGFloat y = self.view.frame.size.height / 10;
    CGFloat w = x * 6;
    CGFloat h = 40;
    CGFloat hEdge = 10;
    CGFloat cornerRadius = 10;

    // 标签
    _labelInfo = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
    _labelInfo.text = @"绿豆猪下载";
    _labelInfo.textColor = [UIColor greenColor];
    _labelInfo.shadowColor = [UIColor redColor];
    _labelInfo.shadowOffset = CGSizeMake(2, 3);
    _labelInfo.textAlignment = NSTextAlignmentCenter;
    NSArray* arrayFont = [UIFont familyNames];
    _labelInfo.font = [UIFont fontWithName:arrayFont[arc4random()%arrayFont.count] size:h-2];
    [self.view addSubview:_labelInfo];

    // 文本信息栏
    y = _labelInfo.frame.origin.y + _labelInfo.frame.size.height;
    y += 2*hEdge;
    _textInfo = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
    _textInfo.placeholder = @"Here is the app infomation list";
    _textInfo.layer.cornerRadius = cornerRadius;
    _textInfo.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_textInfo];

    // 猪猪图标按钮
    UIImage* imageStart = [UIImage imageNamed:@"pig.png"];
    y =  _textInfo.frame.origin.y + _labelInfo.frame.size.height;
    y += hEdge;
    _buttonDownload = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w/4, h*2)];
    [_buttonDownload setImage:imageStart forState:UIControlStateNormal];
    _buttonDownload.backgroundColor = [UIColor clearColor];
    [_buttonDownload addTarget:self action:@selector(onClickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonDownload];

    // 下载指示器
    _activityIndicatorDownload = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(x+w/2, y, w/4, h)];
    //[_activityIndicatorDownload startAnimating];
    [self.view addSubview:_activityIndicatorDownload];

    // 下载开关
    _switchDownload = [[UISwitch alloc] initWithFrame:CGRectMake(x+3*w/4, y, w/4, h)];
    [_switchDownload addTarget:self action:@selector(onSwitchChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_switchDownload];

    // 下载进度条
    _processViewDownload = [[UIProgressView alloc] initWithFrame:CGRectMake(x + w/4, y + h, 3*w/4, h)];
    _processViewDownload.progress = 0;
    [self.view addSubview:_processViewDownload];

    // 下载文件指示器
    y = _buttonDownload.frame.origin.y + _buttonDownload.frame.size.height;
    y += hEdge;
    _sliderDownload = [[UISlider alloc] initWithFrame:CGRectMake(x, y, w, h)];
    [self.view addSubview:_sliderDownload];
}

-(IBAction)onClickBtn:(id)sender
{
    _textInfo.text = @"This is green pig app.";
}

-(IBAction)onSwitchChange:(id)sender
{
    // 启动时显示进度控件,停止时隐藏控件
    BOOL bHide = !_switchDownload.isOn;
    [_sliderDownload setHidden:bHide];
    [_activityIndicatorDownload setHidden:bHide];
    [_processViewDownload setHidden:bHide];
    if(_switchDownload.isOn)
    {
        _sliderDownload.value = 0;
        _sliderDownload.minimumValue = 0;
        _sliderDownload.maximumValue = arc4random()%1000 + 10;
        _textInfo.text = @"strat download .....";
        _timerDownload = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onDownload) userInfo:nil repeats:YES];
        [_activityIndicatorDownload startAnimating];
        _processViewDownload.progress = 0;
    }
    else
    {
        [_timerDownload invalidate];
        [_activityIndicatorDownload stopAnimating];
        _textInfo.text = @"Download finished.";
    }
}

-(IBAction)onDownload
{
    _processViewDownload.progress += arc4random()%100 / 1000.0f;
    _sliderDownload.value = _processViewDownload.progress * _sliderDownload.maximumValue;
    _textInfo.text = [NSString stringWithFormat:@"%.2f%%  %.2fM  %.2fM", _processViewDownload.progress*100, _sliderDownload.value, _sliderDownload.maximumValue];
    if(_processViewDownload.progress >= 1)
    {
        [_switchDownload setOn:NO animated:YES];
        [self onSwitchChange:_switchDownload];
    }
}

代码链接

时间: 2024-10-10 00:10:36

NSTimer,UIActivityIndicatorView,UIProgressView等控件额使用方法的相关文章

【转】带checkbox的ListView实现(二)——自定义Checkable控件的实现方法

原文网址:http://blog.csdn.net/harvic880925/article/details/40475367 前言:前一篇文章给大家展示了传统的Listview的写法,但有的时候我们并不想在DataHolder类中加一个标识是否选中的checked的成员变量,因为在项目开发中,大部分的ListItemLayout布局都是大家共用的,有些人根本不需要checkbox控件,所以会在初始化的时候把这个控件给隐藏掉,但我们的DataHolder在构造的时候以及ListItemAdapt

设定当前视图中所有控件字体的方法

     本范例实现的是对界面中所有的控件一次性的设置字体样式.思路是找到父控件,然后遍历子控件.如果子控件是可以修改文字的控件,那么就设置文字.这用到了控件的继承,很多控件都是继承与textview的,所以将控件均转为textview,最后设置字体即可. 布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.

DevExpress的Web控件汉化方法

原文:DevExpress的Web控件汉化方法 项目中用到devexpress的web控件,机器没有安装devexpress控件,直接在项目中引用的dev的dll,项目运行时发现都是英文界面,所以解决汉化问题. 项目中用的是v10.2版本,以此版本为例,其他版本方法应该类似. 步骤如下:   1.从官网下载对应的汉化包,地址:http://www.devexpresscn.com/news/DevExpress-news-94.html,找到对应的版本下载. 2.在下载的汉化文件里找到\Web\

VC ON_CONTROL_RANGE多个控件响应一个方法

步骤/方法 分三个步骤 在头文件中声明函数例如 afx_msg void onNum(UINT uID) 在.cpp文件中添加函数体 void CCalculatorDlg::OnNum(UINT uID) { UINT index=uID-IDC_NUM_0; CString num; num.Format(_T("%d"),index); AfxMessageBox(num); } 4 添加消息映射 ON_CONTROL_RANGE(BN_CLICKED,IDC_NUM_0,IDC

AspNetPager控件分页使用方法

AspNetPager控件官方下载地址:http://www.webdiyer.com/aspnetpager/ 把控件加到项目中(添加自定义控件的方法),并把它拖放到页面上 <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="

C# CheckedListBox控件的使用方法

1. 加入项 checkedListBox1.Items.Add("蓝色"); checkedListBox1.Items.Add("红色"); checkedListBox1.Items.Add("黄色"); 2. 推断第i项是否选中,选中为true,否则为false if(checkedListBox1.GetItemChecked(i)) {      return true; } else {      return false; } 3

WPF送走控件的focus方法

我们可以调用Focus()方法,让WPF控件获得焦点, 那我现在不想要焦点了, 如何把这个包袱抛出去? 可以,  恩, 没有Unfocus(), 但下面的方法也许可行(把焦点抛给另一个不知道的控件): myControl.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); WPF送走控件的focus方法,布布扣,bubuko.com

web页面动态加载UserControl,并调用用户控件中的方法来初始化控件

1,HTML页 头部注册: <%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/CeShiBu.ascx" TagPrefix="UserControl" TagName="CeShiBu"%> <%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/KaiFaBu.ascx" TagPrefix=&quo

UISlider控件属性及方法(转)

初始化一个Slider   UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 400,320 , 20)];   访问UISlider的值      slider.value = 3;  //设置slider的值 float value = slider.value;  //得到slider的值 [slider setValue:3 animated:YES];  //设置slider的值      slider.mi