ipad 、iphone开发-通过定时器显示进度条

ipad 开发-通过定时器显示进度条(练习定时器和进度条的用法)

2011/10/29 21:56

此文章原创,转载请注明来自本博客

网上ipad开发的教程太少了,后面一段时间我会将自己的一些学习心得发布出来,和大家一起来共同学习IPAD开发,同时也为自己以后的开发作一些笔记。

xcodeipad准备了一个定时器对象  NSTimer,通过定时器调用,就可以实现动态的更改进度条,当然,实际使用中,应该是通过定时器,检测下载进度,在来动态更新进度条。

第一步:创建一个view_based application, ipad ,  名称取为timertest

每二步:修改timertestAppDelegate.h

#import <UIKit/UIKit.h>

@class timertestViewController;

@interface timertestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    timertestViewController *viewController;
    UIProgressView *progressView;}

@property (nonatomic, retain) IBOutlet UIWindow *window;@property (nonatomic,retain) IBOutlet UIProgressView *progressView;@property (nonatomic, retain) IBOutlet timertestViewController *viewController;

@end

标红的是我加入的代码,这一步主要是加入了 UIProgressView

第三步:修改timertestAppDelegate.m

#import "timertestAppDelegate.h"#import "timertestViewController.h"

@implementation timertestAppDelegate

//  声明一个常量用 define

#define DOWNLOAD_TIMEOUT    60.0static CGFloat amt=0.0;


@synthesize window;@synthesize progressView;@synthesize viewController;


#pragma mark -#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //程序启动时执行。。。这个事件
    // Override point for customization after app launch.
    [self.window addSubview:viewController.view];
   
  //id 我记的是用来声明指针对象。。
    id timer;

// 这里调 用了NSTimer对象,repeat:YES表示每隔一段时间就要执行一次,如果值为NO就表示只执行一次哦

//这里指定了处理器,handleTimer

    timer=[NSTimer scheduledTimerWithTimeInterval:0.5
                                           target:self
                                         selector:@selector(handleTimer:)
                                         userInfo:nil
                                          repeats:YES];

//动态创建progressView

    progressView=[[UIProgressView alloc] initWithFrame:CGRectMake(30.0f,80.0f,255.0f,90.0f)];

// view里加入动态创建的这个控件

    [self.viewController.view addSubview:progressView];
    [progressView setProgressViewStyle:UIProgressViewStyleBar];

//init alloc这种用法的,都需要release哦,切记

    [progressView release];
   
    [self.window makeKeyAndVisible];   
   
    return YES;}

-(void)handleTimer:(id)atimer{
   
    amt+=1;
    if(progressView!=nil){
        [progressView setProgress:(amt/DOWNLOAD_TIMEOUT)];
        if(amt>DOWNLOAD_TIMEOUT){
            [atimer invalidate];
            atimer=nil;
        }

    }
    
    }


- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */}


- (void)applicationDidBecomeActive:(UIApplication *)application {

}


- (void)applicationWillTerminate:(UIApplication *)application {

}


#pragma mark -#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
  
}


- (void)dealloc {
    
    [viewController release];
    [window release];
    [super dealloc];}


@end

 

 原文地址:http://hi.baidu.com/flwblog/blog/item/edb49201a9cc24ca267fb568.html 

 

你可能感兴趣的:(iPhone开发)