首页 > 代码库 > 定时器和进度条(IOS开发)

定时器和进度条(IOS开发)

详见注释哈!


- (IBAction)startToMove:(id)sender {
    // 判断是否在旋转
    // stopAnimating方法为停止动画效果
    if ([self.myActivityIndicatorView isAnimating]) {
        [self.myActivityIndicatorView stopAnimating];
    }
    else
    {
        [self.myActivityIndicatorView startAnimating];
    }
}


- (IBAction)downloadProgress:(id)sender {
    // 定时器方法:在一个特定的时间间隔后向某对象发送消息
    // target 为发送消息给哪个对象
    // timeinterval 间隔时间
    // selector 要调用的方法名
    // userinfo 给消息发送的参数
    // repeats 是否重复
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                               target:self
                                               selector:@selector(download)
                                               userInfo:nil
                                              repeats:YES];
}

- (void)download{
    self.myProgressView.progress += 0.1; // 设定步进长度
    if (self.myProgressView.progress == 1.0) {// 如果进度条到头了
        // 终止定时器
        [myTimer invalidate];
        // 弹出对话框
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"download completed!"
                                             message:@"Hey!Lucy!"
                                             delegate:nil
                                             cancelButtonTitle: @"OK!"otherButtonTitles:nil, nil];
        [alert show];
    }
}


定时器和进度条(IOS开发)